Professional Documents
Culture Documents
Inheritance & Overriding
Inheritance & Overriding
Inheritance & Overriding
Inheritance
Inheritance in java is a mechanism in which one object
acquires all the properties and behaviors of parent object.
The idea behind inheritance in java is that you can create new
classes that are built upon existing classes.
When you inherit from an existing class, you can reuse
methods and fields of parent class, and you can add new
methods and fields also.
Inheritance represents the IS-A relationship, also known as
parent-child relationship.
Syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
obj.msg();
}
}
Simple Inheritance
class A{
protected int x=5;
public void msg()
{
System.out.println("In Class A \n x="+x);}
}
class C extends A
OUTPUT
{
public static void main(String args[]){ In Class A
C obj=new C(); X=5
obj.msg();
}
}
Simple Inheritance
class A{
private int x=5;
public void msg()
{
System.out.println("In Class A \n x="+x);}
}
class C extends A
OUTPUT
{
public static void main(String args[]){ In Class A
C obj=new C(); X=5
obj.msg();
} Variables that are declared private can be
} accessed outside the class, if public getter
methods are present in the class.
Multilevel Inheritance
class A{ class C extends B
public void msg() {
{ public static void main(String args[])
System.out.println("In Class A"); } } {
C obj=new C();
class B extends A obj.msg();
{ }
public void msg() } OUTPUT
{
In Class B
System.out.println("In Class B"); } }
Hierarchical Inheritance
class A{ class D
public void msg() {
{ public static void main(String args[])
System.out.println("In Class A"); } } {
C obj1=new C(); obj1.msg();
class B extends A B obj2=new B(); obj2.msg();
{ A obj3=new A(); obj3.msg();
public void msg() }
{ }
System.out.println("In Class B"); } }
OUTPUT
class C extends A
{ In Class C
public void msg() { In Class B
System.out.println("In Class C"); } } In Class A
Aggregation in Java
If a class have an entity reference, it is known as Aggregation.
Aggregation represents HAS-A relationship.
Consider the following situation
Employee object contains many informations such as id, name, emailId
etc.
It contains one more object named address, which contains its own
informations such as city, state, country, zipcode etc.
In such case, Employee has an entity reference address, so relationship
is Employee HAS-A address.
Example
public class Address { void display(){
String city, state, country; System.out.println(id+" "+name);
public Address(String city, String state, String System.out.println(address.city+" "+address.st
country) { ate+" "+address.country);
this.city = city; this.state = state; }
this.country = country; } }
public static void main(String[] args) {
public class Emp { Address address1=new Address("gzb","UP","i
int id; String name; Address address; ndia");
public Emp(int id, String name, Emp e=new Emp(111,"varun",address1);
Address address) { e.display(); } }
this.id = id; this.name = name;
this.address=address; }
Super Keyword
The super keyword in java is a reference variable which is used
to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of
parent class is created implicitly which is referred by super
reference variable.
Usage of java super Keyword
super can be used to refer immediate parent class instance variable.
super can be used to invoke immediate parent class method.
super() can be used to invoke immediate parent class constructor.
super is used to refer immediate parent class instance
variable.
class A class B extends A
{ {
int x=5; int x=10;
public void put() public void put()
{ {
System.out.println("x="+x); System.out.println("x="+x);
} System.out.println("Superclass
} x="+super.x);
}
public static void main(String args[])
{
B ob=new B();
ob.put(); } }
super can be used to invoke parent class method
class A class B extends A
{ {
int x=5; int x=10;
public void put() public void put()
{ {
System.out.println("x="+x); System.out.println("x="+x);
} super.put();
} }
public static void main(String args[])
{
B ob=new B();
ob.put(); } }
super is used to invoke parent class constructor
class A class B extends A
{ {
int x; int x;
A(int z) B()
{ {
x=5; super(5);
System.out.println("x="+x); x=10;
} System.out.println("x="+x); }
} public static void main(String args[])
{
B ob=new B(); } }
Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple
inheritance is not supported in java.
Consider a scenario where A, B and C are three classes.
The C class inherits A and B classes.
If A and B classes have same method and you call it from child class
object, there will be ambiguity to call method of A or B class.
Since compile time errors are better than runtime errors, java renders
compile time error if you inherit 2 classes.
So whether you have same method or different, there will be compile
time error now.
Example
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B
OUTPUT
{
public static void main(String args[]){ Compile time error
C obj=new C(); ‘{‘ expected
obj.msg();
}
}
Runtime polymorphism
Runtime polymorphism or Dynamic Method Dispatch is
a process in which a call to an overridden method is resolved at
runtime rather than compile-time.
In this process, an overridden method is called through the
reference variable of a superclass.
The determination of the method to be called is based on the
object being referred to by the reference variable.
Runtime polymorphism
Superclass reference variable can refer to a subclass object.
Java uses this fact to resolve calls to overridden methods at run time.
When an overridden method is called through a superclass reference, Java determines which
version of that method to execute based upon the type of the object being
referred to at the time the call occurs.
Thus, this determination is made at run time.
When different types of objects are referred to, different versions of an overridden
method will be called.
In other words, it is the type of the object being referred to that determines which version of an
overridden method will be executed.
Therefore, if a superclass contains a method that is overridden by a subclass, then
when different types of objects are referred to through a superclass reference
variable, different versions of the method are executed.
Upcasting
When reference variable of Parent class refers to the object of Child class, it
is known as upcasting.
class A{}
class B extends A{}
A a=new B();
Example
class A { class Dispatch {
void callme() { public static void main(String args[]) {
System.out.println("Inside A's callme A a = new A();
method"); } }
B b = new B();
C c = new C();
class B extends A {
A r;
void callme() {
System.out.println("Inside B's callme r = a;
method"); } } r.callme();
r = b;
class C extends A { r.callme();
void callme() { r = c;
System.out.println("Inside C's callme r.callme(); } }
method"); } }
Final Keyword
Java final variable - If you make any variable as final, you cannot change
the value of final variable(It will be constant).
Java final method - If you make any method as final, you cannot
override it.
Java final class - If you make any class as final, you cannot extend it.
Is final method inherited?
Yes, final method is inherited but you cannot override it.
Blank or uninitialized Variable
class C
{
final int x;
public void put()
{
System.out.println("x="+x);
OUTPUT
}
Compile time error
public static void main(String args[])
{ Variable x is not initialized
C ob=new C();
ob.put();
}
}
Example
class C
{
final int x; We can initialize blank final
C(){ variable but only in constructor.
x=10;
}
public void put(){
System.out.println("x="+x);
}
class A{
void cube(final int n){
n=n+2;
n*n*n; OUTPUT
} Compile time error
public static void main(String args[]){
A b=new A ();
b.cube(5);
}
}