Unit 4
Unit 4
Vehicle
Car
Java has four type of access specifier private, public, protected and
default . There are different access rules for all these access
specifier in inheritance.
Private data member
private data member can only be accessed in same class where
they are declared. Child class don't have access to parent class
private data member.
class base
{
private int a;
}
class der extends base
{
void print()
{
System.out.println("a:"+a); // Error: private data can't be accessed.
}
}
public data member
public data member of parent class can be accessed in
child class.
class base
{
public int a;
}
class der extends base
{
void print()
{
System.out.println("a:"+a);
}
}
Above program wil be compiled successfully.
protected data member
Protected data member are same as private
with one exception that they can be accessed in
child class.
class base
{
protected int a;
}
class der extends base
{
void print()
{
System.out.println("a:"+a);
}
}
Above program wil be compiled successfully.
Default data member
Words Dictionary
- definitions : int
+ main (args : String[]) :
void + definitionMessage() :
void
Constructor in inheritance
When a class inherit another class then child class depends on
parent class data. It make sense that before initialising child class
object, parent class object need to be initialised. In java whenever
child class constructor is called, it make a call to parent class constr
class one
{
one()
{
System.out.println("one class constructor");
}
}
class two extends one
{
two()
{
System.out.println("two class constructor");
}
public static void main(String arg[])
{
two t =new two();
}
}
/*Output:
one class constructor
two class constructor
*/
Super Keyword
The super keyword in java is a reference variable
that is used to refer immediate parent class object.
Whenever you create the instance of subclass, an
instance of parent class is created implicitly i.e.
referred by super reference variable.
Usage of java super Keyword
super is used to refer immediate parent class
instance variable.
super() is used to invoke immediate parent class
constructor.
super is used to invoke immediate parent class
method.
Super Keyword class B extends A
super keyword is used to call { int b;
constructor of parent class. In B()
child class super keyword used {
to refer parent class. System.out.println("B's Default
constructor");
class A }
B(int y)
{ {
int a; super(y); // It will call parent class
A() parameterised constructor
b=y;
{ System.out.println("B's parameterised
System.out.println("A's Default constructor ");
constructor "); }
} public static void main(String arg[])
{
A(int x) B b1 =new B();
{a=x; B b2=new B(12)
System.out.println("A's parameterised }}
constructor "); /*Output:
} A's Default constructor
B's Default constructor
} A's parameterised constructor
B's parameterised constructor
*/
The super Reference
Constructors are not inherited, even though
they have public visibility
Yet we often want to use the parent's
constructor to set up the "parent's part" of
the object
The super reference can be used to refer to
the parent class, and often is used to invoke
the parent's constructor
The super Reference
A child’s constructor is responsible for
calling the parent’s constructor
If the child constructor invokes the parent
(constructor) by using the super reference, it
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
Multilevel Inheritance
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Hierarchical Inheritance
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Multiple Inheritance
Java supports single inheritance, meaning that a
derived class can have only one parent class
Multiple inheritance allows a class to be derived
from two or more classes, inheriting the
members of all parents
Collisions, such as the same variable name in
two parents, have to be resolved
Java does not support multiple inheritance
In most cases, the use of interfaces gives us
aspects of multiple inheritance without the
overhead
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.
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were
class Bike{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike obj=new Bike();
obj.run();
}
}//end of class
2) Java final method
If you make any method as final, you cannot override it.
Example of final method
class Bike{
final void run(){System.out.println("running");}
}