Java M3
Java M3
https://www.programiz.com/java-programming/online-
compiler/
Programming Experiment -1
Develop a JAVA program to add TWO matrices of suitable order N(The value
of N should be read from command line arguments.)
Inheritance
In Java, Inheritance is an important pillar of OOP(Object-Oriented
Programming).
A class that inherits from another class can reuse the methods and
fields of that class.
In addition, you can add new fields and methods to your current
class as well.
Inheritance
Inheritance Basics
Using the extends keyword indicates you are derived from an existing class.
Syntax :
Using super
Whenever a subclass needs to refer to its immediate superclass, it can do so
by use of the keyword super.
The second is used to access a member of the superclass that has been
hidden by a member of a subclass.
The second form of super acts somewhat like this, except that it always refers to
the superclass of the subclass in which it is used.
super. Member
However, you can build hierarchies that contain as many layers of inheritance
as you like.
To see how a multilevel hierarchy can be useful, consider the following program.
In it, the subclass BoxWeight is used as a superclass to create the subclass called
Shipment.
Shipment inherits all of the traits of BoxWeight and Box, and adds a field called
cost, which holds the cost of shipping such a parcel.
When a class hierarchy is created, in what order are the constructors for the
classes that make up the hierarchy executed?
For example, given a subclass called B and a superclass called A, is A’s constructor
executed before B’s, or vice versa?
The answer is that in a class hierarchy, constructors complete their execution in order of
derivation, from superclass to subclass.
Further, since super( ) must be the first statement executed in a subclass’ constructor,
this order is the same whether or not super( ) is used.
If super( ) is not used, then the default or parameter less constructor of each superclass
will be executed.
Method Overriding
In a class hierarchy, when a method in a subclass has the same name and
type signature as a method in its superclass, then the method in the subclass
is said to override the method in the superclass.
Programming Experiment -2
Method overriding forms the basis for one of Java’s most powerful
concepts: dynamic method dispatch.
Interfaces
An Interface in Java programming language is defined as an abstract type used to specify the
behavior of a class.
An interface in Java is a blueprint of a behavior. A Java interface contains static constants and
abstract methods.
Interfaces
Implementing Interfaces
Nested Interfaces
Variables in Interfaces
When a class implements an interface that inherits another interface, it must provide
implementations for all methods required by the interface inheritance chain.
Example:
InterfaceName.staticMethodName
Notice that this is similar to the way that a static method in a class is called.
Sometimes you will want to prevent a class from being inherited. To do this,
precede the class declaration with final. Declaring a class as final implicitly
declares all of its methods as final, too.
As you might expect, it is illegal to declare a class as both abstract and final
since an abstract class is incomplete by itself and relies upon its subclasses to
provide complete implementations.