MODULE -3
https://www.programiz.com/java-programming/online-
compiler/
Tuesday 26 November 2024 . 1
MODULE -3
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.)
Tuesday 26 November 2024 . 2
MODULE -3
Tuesday 26 November 2024 . 3
MODULE -3
Tuesday 26 November 2024 . 4
MODULE -3
Tuesday 26 November 2024 . 5
MODULE -3
Tuesday 26 November 2024 . 6
MODULE -3
Tuesday 26 November 2024 . 7
MODULE -3
Tuesday 26 November 2024 . 8
MODULE -3
Inheritance
In Java, Inheritance is an important pillar of OOP(Object-Oriented
Programming).
It is the mechanism in Java by which one class is allowed to inherit
the features(fields and methods) of another class.
In Java, Inheritance means creating new classes based on existing
ones.
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.
Tuesday 26 November 2024 . 9
MODULE -3
Tuesday 26 November 2024 . 10
MODULE -3
Inheritance
Inheritance is one of the cornerstones of object-oriented programming because it
allows the creation of hierarchical classifications.
In the terminology of Java, a class that is inherited is called a superclass.
The class that does the inheriting is called a subclass.
Therefore, a subclass is a specialized version of a superclass. It inherits all of the
members defined by the superclass and adds its own, unique elements.
Tuesday 26 November 2024 . 11
MODULE -3
Inheritance Basics
How to Use Inheritance in Java?
The extends keyword is used for inheritance in Java.
Using the extends keyword indicates you are derived from an existing class.
In other words, “extends” refers to increased functionality.
Syntax :
class derived-class extends base-class
{
//methods and fields
}
Tuesday 26 November 2024 . 12
MODULE -3
Tuesday 26 November 2024 . 13
MODULE -3
Types of inheritance in java
Creating a Multilevel Hierarchy
Tuesday 26 November 2024 . 14
MODULE -3
Tuesday 26 November 2024 . 15
MODULE -3
Tuesday 26 November 2024 . 16
MODULE -3
Member Access and Inheritance
a subclass includes all of the members of its superclass, it cannot
access those members of the superclass that have been declared as
private.
Tuesday 26 November 2024 . 17
MODULE -3
Tuesday 26 November 2024 . 18
MODULE -3
Tuesday 26 November 2024 . 19
MODULE -3
Tuesday 26 November 2024 . 20
MODULE -3
Tuesday 26 November 2024 . 21
MODULE -3
Tuesday 26 November 2024 . 22
MODULE -3
A Superclass Variable Can Reference a Subclass Object
A reference variable of a superclass can be
assigned a reference to any subclass
derived from that superclass.
Tuesday 26 November 2024 . 23
MODULE -3
Using super
Whenever a subclass needs to refer to its immediate superclass, it can do so
by use of the keyword super.
super has two general forms.
The first calls the superclass’ constructor.
The second is used to access a member of the superclass that has been
hidden by a member of a subclass.
Tuesday 26 November 2024 . 24
MODULE -3
Tuesday 26 November 2024 . 25
MODULE -3
Tuesday 26 November 2024 . 26
MODULE -3
A Second Use for super
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.
This usage has the followinggeneral form:
super. Member
Here, member can be either a method or an instance variable.
Tuesday 26 November 2024 . 27
MODULE -3
Tuesday 26 November 2024 . 28
MODULE -3
Creating a Multilevel Hierarchy
Up to this point, we have been using simple class hierarchies that consist of
only a superclass and a subclass.
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.
Tuesday 26 November 2024 . 29
MODULE -3
Tuesday 26 November 2024 . 30
MODULE -3
Tuesday 26 November 2024 . 31
MODULE -3
Tuesday 26 November 2024 . 32
MODULE -3
Tuesday 26 November 2024 . 33
MODULE -3
When Constructors Are Executed
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.
Tuesday 26 November 2024 . 34
MODULE -3
Tuesday 26 November 2024 . 35
MODULE -3
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.
When an overridden method is called from within its subclass, it will
always refer to the version of that method defined by the subclass.
Tuesday 26 November 2024 . 36
MODULE -3
Tuesday 26 November 2024 . 37
MODULE -3
Tuesday 26 November 2024 . 38
MODULE -3
Method overriding occurs only when the names and
the type signatures of the two methods are identical.
If they are not, then the two methods are simply
overloaded.
Tuesday 26 November 2024 . 39
MODULE -3
Programming Experiment -2
Develop a stack class to hold a maximum of 10 integers with
suitable methods. Develop a JAVA main method to illustrate
Stack operations.
Tuesday 26 November 2024 . 40
MODULE -3
Tuesday 26 November 2024 . 41
MODULE -3
Tuesday 26 November 2024 . 42
MODULE -3
Tuesday 26 November 2024 . 43
MODULE -3
Tuesday 26 November 2024 . 44
MODULE -3
Tuesday 26 November 2024 . 45
MODULE -3
Tuesday 26 November 2024 . 46
MODULE -3
Tuesday 26 November 2024 . 47
MODULE -3
Tuesday 26 November 2024 . 48
MODULE -3
Dynamic Method Dispatch
Method overriding forms the basis for one of Java’s most powerful
concepts: dynamic method dispatch.
Dynamic method dispatch is the mechanism by which a call to an
overridden method is resolved at run time, rather than compile time.
Dynamic method dispatch is important because this is how Java
implements run-time polymorphism.
Tuesday 26 November 2024 . 49
MODULE -3
Tuesday 26 November 2024 . 50
MODULE -3
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.
Tuesday 26 November 2024 . 51
MODULE -3
Interfaces
Tuesday 26 November 2024 . 52
MODULE -3
Implementing Interfaces
Once an interface has been defined, one or more classes can
implement that interface. To implement an interface, include the
implements clause in a class definition, and then create the
methods required by the interface.
Tuesday 26 November 2024 . 53
MODULE -3
Nested Interfaces
An interface can be declared a member of a class or another interface. Such an
interface is called a member interface or a nested interface.
A nested interface can be declared as public, private, or protected.
Tuesday 26 November 2024 . 54
MODULE -3
Here is an example that demonstrates a nested interface:
Tuesday 26 November 2024 . 55
MODULE -3
Variables in Interfaces
Tuesday 26 November 2024 . 56
MODULE -3
Interfaces Can Be Extended
One interface can inherit another by use of the keyword extends.
The syntax is the same as for inheriting classes.
When a class implements an interface that inherits another interface, it must provide
implementations for all methods required by the interface inheritance chain.
Tuesday 26 November 2024 . 57
MODULE -3
Example:
Tuesday 26 November 2024 . 58
MODULE -3
Default Method Fundamentals
An interface default method is defined similar to
the way a method is defined by
a class.
The primary difference is that the declaration is
preceded by the
keyword default.
For example, consider this simple interface:
Tuesday 26 November 2024 . 59
MODULE -3
Use static Methods in an Interface
Here is the general form:
InterfaceName.staticMethodName
Notice that this is similar to the way that a static method in a class is called.
Tuesday 26 November 2024 . 60
MODULE -3
Tuesday 26 November 2024 . 61
MODULE -3
Using final with Inheritance
The keyword final has three uses.
First, it can be used to create the equivalent of a named constant.
The other two uses of final apply to inheritance.
Using final to Prevent Overriding
Using final to Prevent Inheritance
Tuesday 26 November 2024 . 62
MODULE -3
Using final to Prevent Overriding
Because meth( ) is declared as final, it cannot be overridden in B. If you
attempt to do so, a compile-time error will result.
Tuesday 26 November 2024 . 63
MODULE -3
Using final to Prevent Inheritance
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.
Tuesday 26 November 2024 . 64
MODULE -3
The Object Class
There is one special class,
Object, defined by Java.
All other classes are
subclasses of Object.
That is, Object is a
superclass of all other classes.
This means that a reference
variable of type Object can
refer to an object of any other
class.
Tuesday 26 November 2024 . 65