Inheritance Java
Inheritance Java
Programming-
Inheritance
INHERITANCE BASICS
1. Reusability is achieved by INHERITANCE
2. Java classes Can be Reused by extending a class. Extending
an existing class is nothing but reusing properties of the
existing classes.
A super class
B <<class>>
B sub class
A interface
B <<class>>
B sub class
Various Forms of Inheritance
Single Inheritance Hierarchical Inheritance
A A X X
B B A B C A B C
B B
C C
C C
Forms of Inheritance
• Mulitiple Inheritance can be implemented by
implementing multiple interfaces not by extending
multiple classes
Example : A C D
class Z extends A implements C , D
{ …………} Z
OK
class Z extends A ,B class Z extends A
extends B
{ {
WRONG WRONG
OR
} }
Defining a Subclass
Syntax :
class <subclass name> extends <superclass name>
{
variable declarations;
method declarations;
}
Access Location
Non-subclasses in Yes No No No
other packages
Inheritance Basics
1. Whenever a sub class object is created ,super class
constructor is called first.
2. If super class constructor does not have any
constructor of its own OR has an unparametrized
constructor then it is automatically called by Java Run
Time by using call super()
3. If a super class has a parameterized constructor then it
is the responsibility of the sub class constructor to call
the super class constructor by call
super(<parameters required by super class>)
4. Call to super class constructor must be the first
statement in sub class constructor
Inheritance Basics
When super class has a Unparametrized constructor
class A
{
A()
{
System.out.println("This is constructor of class A");
}
} // End of class A
class B extends A
{
B() Optional
{
super();
System.out.println("This is constructor of class B");
}
} // End of class B
Cont…..
class inhtest
{
public static void main(String args[])
{
B b1 = new B();
}
}
OUTPUT
This is constructor of class A
This is constructor of class B
class A
{ File Name is xyz.java
A()
{
System.out.println("This is class A");
} /*
} E:\Java>java inherit1
class B extends A This is class A
{ This is class B
B()
{ E:\Java>
System.out.println("This is class B"); */
}
}
class inherit1
{
public static void main(String args[])
{
B b1 = new B();
}
}
class A
{
private A()
{
System.out.println("This is class A");
}
}
class B extends A
{
B()
{
System.out.println("This is class B");
}
} /*
class inherit2 E:\Java>javac xyz1.java
{ xyz1.java:12: A() has private access
public static void main(String args[]) in A
{ {
B b1 = new B(); ^
} 1 error
}
class A
{
private A()
{
System.out.println("This is class A");
}
A()
{
System.out.println("This is class A");
}
} /*
class B extends A E:\Java>javac xyz2.java
{ xyz2.java:7: A() is already defined in
B() A
{ A()
System.out.println("This is class B"); ^
}
xyz2.java:16: A() has private access
}
class inherit2 in A
{ {
public static void main(String args[]) ^
{ 2 errors
B b1 = new B(); */
}}
When Super class has a parametrized constructor.
class A
{
private int a;
A( int a) B b1 = new B(10,8.6);
{
this.a =a;
System.out.println("This is constructor of class A");
}
}
class B extends A
{
private int b;
D:\java\bin>javac
private double c;
B(int b,double c) inhtest.java
{ inhtest.java:15: cannot find
this.b=b; symbol
this.c=c;
symbol : constructor A()
System.out.println("This is constructor of class B");
} location: class A
} {
^
1 errors
class A
{
private int a;
A( int a)
{
this.a =a;
System.out.println("This is
constructor of class A");
}}
class B extends A
{ B b1 = new B(8,10,8.6);
private int b;
private double c;
B(int a1,int b,double c)
{
OUTPUT
super(a1); This is constructor of class A
this.b=b; This is constructor of class B
this.c=c;
System.out.println("This is
constructor of class B");
}}
class A class B extends A
{ {
private int a; int b;
protected String name; double c;
A(int a, String n) B(int a,String n,int b,double c)
{
{
this.a = a;
this.name = n;
super(a,n);
} this.b=b;
void print() this.c =c;
{ }
System.out.println("a="+a); void show()
} {
} //System.out.println("a="+a);
print();
System.out.println("name="+name);
a is private in System.out.println("b="+b);
class A System.out.println("c="+c);
}
Call to print() }
from super
class A Accessing name field from
super class (super.name)
class xyz3
{
public static void main(String args[])
{
B b1 = new B(10,"OOP",8,10.56);
b1.show();
}
}
E:\Java>java xyz3
a=10
name=OOP
b=8
c=10.56
Thank You