Inheritance, Package, Interface and Exception Handling 3.1 Inheritance
Inheritance, Package, Interface and Exception Handling 3.1 Inheritance
JAVA CH-3
DEPARTMENT OF MCA
Flying
Birds
Attributes
Eagle
Pigeon
Attrib
utes
Attrib
utes
Pengui
n
Attrib
utes
Kiwi
Attrib
utes
class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
superOb.i = 10; superOb.j = 20;
superOb.showij();
/* The subclass has access to all public members of its
superclass. */
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
subOb.showij();
subOb.showk();
subOb.sum();
}
}
E-Mail: [email protected]
SEM-III
JAVA CH-3
DEPARTMENT OF MCA
The general form of a class declaration that inherits a superclass is shown here:
class subclass-name extends superclass-name
{
// body of class
}
o You can only specify one superclass for any subclass that you create.
o Java does not Support the inheritance of multiple super-classes into a single subclass.
o You can, as stated, create a hierarchy of inheritance in which a subclass becomes a superclass
of another subclass. However, no class can be a superclass of itself
// Create a superclass.
class A {
int i; // public by default
private int j; // private to A
void setij(int x, int y) {
i = x;
j = y;
}}
// A's j is not accessible here.
class B extends A {
int total;
void 2.1.3
sum() Declaring
{
Objects:
total = i + j; // ERROR, j is not accessible here
} Box mybox = new Box();
}
OR
class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println("Total is " + subOb.total);
}
}
o This program will not compile because the
reference to j inside the sum( ) method of B
causes an access violation.
o Since j is declared as private, it is only
accessible by other members of its own
class. Subclasses have no access to it.
E-Mail: [email protected]
SEM-III
JAVA CH-3
DEPARTMENT OF MCA
E-Mail: [email protected]
SEM-III
JAVA CH-3
DEPARTMENT OF MCA
class base
{
int a,b;
base(int x,int y)
{
a=x;
b=y;
}
}
class sub extends base
{
int c;
Use: This second form of super is most applicable to situations in which member names
of a subclass hide members by the same name in the superclass.
Note:
Super can also be used to call methods that are hidden by a subclass.
E-Mail: [email protected]
SEM-III
JAVA CH-3
DEPARTMENT OF MCA
Although the instance variable i in B hides the I in A, super allows access to the i defined in
the superclass.
Note: Super can also be used to call methods that are hidden by a subclass.
class a
{
int i;
}
class b extends a
{
int j;
}
class c extends b
{
int k;
}
class inh_hy
{
public static void main(String args[])
{
c obj= new c();
obj.i=10;
obj.j=20;
obj.k=30;
System.out.println("i="+obj.i + "j="+obj.j+ "k="+obj.k);
}
}
Output: i= 10 j=20 k=30
When a class hierarchy is created, in what order are the constructors for the classes that
make up the hierarchy called?
For example, given a subclass called B and a superclass called A, is As constructor called
before Bs, or vice versa?
The answer is that in a class hierarchy, constructors are called 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 parameterless constructor of each superclass will be
executed
E-Mail: [email protected]
SEM-III
JAVA CH-3
class A {
A() {
System.out.println("Inside A's constructor.");
}
}
class B extends A {
B() {
System.out.println("Inside B's constructor.");
}
}
class C extends B {
C() {
System.out.println("Inside C's constructor.");
}
}
DEPARTMENT OF MCA
class CallingCons
{
public static void main(String args[])
{
C c = new C();
}
}
OUTPUT:
Inside As constructor
Inside Bs constructor
Inside Cs constructor
If you think about it, it makes sense that constructors are executed in order of derivation.
Because a superclass has no knowledge of any subclass, any initialization it needs to perform
is separate from and possibly prerequisite to any initialization performed by the subclass.
Therefore, it must be executed first.
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 a subclass, it will always refer to the
method defined by the subclass.
The version of the method defined by the superclass will be hidden.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
void show() {
System.out.println("i and j: " + i + " " + j);
}
} // end of class A
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
Lecturer: Syed Khutubuddin Ahmed
E-Mail: [email protected]
SEM-III
JAVA CH-3
DEPARTMENT OF MCA
When show( ) is invoked on an object of type B, the version of show( ) defined within B is
used.
That is, the version of show( ) inside B overrides the version declared in A.
If you wish to access the superclass version of an overridden method, you can do so by using
super.
For example, in this version of B, the superclass version of show( ) is invoked within the
subclass version. This allows all instance variables to be displayed.
An Example program showing the solution to the above problem:
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
void show() {
System.out.println("i and j: " + i + " " + j);
}
} // end of class A
class B extends A {
int k;
B(int a, int b, int c)
{
super(a, b);
k = c;
}
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
OUTPUT:
i and j : 1 2
k: 3
NOTE:
o 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.
o The concept of overriding happen with a condition inheritance is the mandatory.
E-Mail: [email protected]