U3A Java_Inheritance
U3A Java_Inheritance
in
JAVA
SUBJECT: 4341602 Object Oriented Programming with Java
SEM: IV (Diploma Information Technology)
id id
name name
address address
marks
setId() setId()
getId() getId()
setName() setName()
getName() getName()
setAddr() setAddr()
getAddr() getAddr()
setMark()
getMark()
Chintan Gajjar, ITD, Dr S & S S Ghandhy College, Surat
Explanation
When an object to class is created, it contains a copy of
class within it. This means there is a relation between
class and class objects.
This is the reason why class members are available to
class. Note that we do not create class object, but still a copy of
it is available to class object.
Here, we can observe that Student class-2 is smaller and easier to
develop. By using inheritance programmer can develop the classes very
easily. Hence programmer’s productivity is increased.
The main advantage of inheritance is code reusability. A programmer
reuses the super class code without rewriting it, in creation of subclasses.
We can only specify one superclass for any subclass that you create. Java
does not support the inheritance of multiple superclasses into a single
subclass.
We can create a hierarchy of inheritance in which a subclass becomes a
superclass of another subclass. However, no class can be a superclass of
itself.
Remember, once you have created a superclass that defines the general
aspects of an object, that superclass can be inherited to form specialized
classes. Each subclass simply adds its own unique attributes. This is the
essence of inheritance.
Chintan Gajjar, ITD, Dr S & S S Ghandhy College, Surat
'super' keyword
REMEMBER
A class member that has been declared as private will remain private to its
class. It is not accessible by any code outside its class, including subclasses.
A A A
B B D C
B
Single E F
Inheritance C
Multi-level Hierarchical Inheritance
Inheritance
Chintan Gajjar, ITD, Dr S & S S Ghandhy College, Surat
Multi-level Inheritance
Rectangle
We can build hierarchies that contain as many layers length
of inheritance as you like. As mentioned, it is width
Rectangle()
perfectly acceptable to use a subclass as a superclass Rectangle(int,int)
area()
of another.
RectWeight
For example, given three classes called Rectangle, length
RectWeight and Shipment, Shipment can be a width
weight
subclass of RectWeight, which is a subclass of area()
Rectweight()
Rectangle. RectWeight(int,int,int)
Prac-18_B
Chintan Gajjar, ITD, Dr S & S S Ghandhy College, Surat
Explanation
Here, the subclass RectWeight is used as a superclass to create the subclass
called Shipment. Shipment inherits all of the traits of RectWeight and
Rectangle, and adds a field called cost, which holds the cost of shipping such a
parcel.
Because of inheritance, Shipment can make use of the previously defined
classes of Rectangle and RectWeight, adding only the extra information it
needs for its own, specific application.
This is part of the value of inheritance; it allows the reuse of code. This example
illustrates one other important point: super( ) always refers to the constructor
in the closest superclass. The super( ) in Shipment calls the constructor in
RectWeight. The super( ) in RectWeight calls the constructor in Rectangle.
In a class hierarchy, if a superclass constructor requires parameters, then all
subclasses must pass those parameters "up the line." This is true whether or
not a subclass needs parameters of its own.
Chintan Gajjar, ITD, Dr S & S S Ghandhy College, Surat
Hierarchical Inheritance
regular contract
daily wages
Prac-18_C
Chintan Gajjar, ITD, Dr S & S S Ghandhy College, Surat
Hierarchical Inheritance Example-2
class Staff{ class Typist extends Staff{
protected int code; int speed;
protected String name; void getdata(int c,String nm,int sp){
void getdata(int c,String nm){ super.getdata(c,nm);
code = c; speed = sp;
name = nm; }
} void show(){
void show(){ super.show();
System.out.println("Staff Code:"+code); System.out.println("Typist's Speed:"+speed);
System.out.println("Staff Name: "+name); }
} }
}
class Figure
class Rectangle extends Figure
class Triangle extends Figure