Inhertiance Topic
Inhertiance Topic
class StaticDemo
{
static int a=10;
static int b;
static
{
System.out.println("In the static block");
b=90;
}
Both of these statements will define or declare the two static data items. The
variable which we have declared so far in the class definition are known as Instance
variables.
e.g.
class Sample
{
int i;
static int j;
}
Now , the data item i will be considered as instance variable because whenever
create an object of the class Sample a separate copy of the i is created for every
object.
ob1 ob2
i j i
But the data item j which is a static data item, only one copy is maintained for j.
The static data items are known as class variables because only one copy is
maintained for the entire class.
(b) static
{
::::
}
The group of statements enclosed in the Curley braces are considered as a block.
And it is a static block. The static block is executed first, even before the execution
of the main( ) method.
it is a static function .
Some its limitations are ,
output :
In static block
In main( ) method
a=10
b=90
class Static2
{
int a;
static int b;
void showAll( )
{
System.out.println("a=" + a + " b="+ b);
}
}
class Static2Demo
{
public static void main(String args[])
{
Static2 ob1=new Static2( );
ob1.setAll(5,6);
ob1.showAll( );
ob2.setAll(3,2);
ob2.showAll( );
ob1.showAll( );
}
}
ob1 ob2
a b a
5 6 3
2
output:
a=5 b=6
a=3 b=2
a=5 b=2
Inheritance :
==========
The term "Inheritance" refers to creating a new class on the basis of the existing
class. The new class is known as the derived class and the existing class is known
as the base class.
The derived class have more or less all fetures of the base class and some new
features of its own.
The general form is ,
In the case of Single Inheritance , we derive the single derived class , on the basis
of the single base class.
class A
{
private int a;
ob.setA(5);
ob.setB(10);
ob.showA();
ob.showB();
}
}
private members :
In the case of inheritance , the private members have the following features,
(i) These members are not directly accessible by the object of a class.
e.g.
ob.b=10; is Invalid
That is, they cannot be used in the derived class or by the object of the derived
class.
showB()
System.out.println("a="+a);
it will be invalid
public members :
In the case of inheritance, the public members have the following features,
e.g.
ob.setA(5) ; is valid
That is, they can be used in the derived class or by the object of the derived class.
B ob=new B();
ob.showA();
In the case of the mutilevel inehritance , we first create a derived class on the basis
of the single base class.Then,on the basis of this derived class , we create another
derived class and so on...
class A
{
private int a;
class MutiLevel
{
public static void main(String args[ ])
{
C ob=new C( );
ob.setA(5);
ob.setB(10);
ob.setC(15);
ob.showA();
ob.showB();
ob.showC();
}
}
class HLevel
{
public static void main(String args[ ])
{
B ob1=new B( );
ob1.setA(5);
ob1.setB(10);
ob1.showA();
ob1.showB();
C ob2=new C( );
ob2.setA(19);
ob2.setC(15);
ob2.showA();
ob2.showC();
}
}
(d) Hybrid Inheritance: It is a mixture of two or more types of inheritance.
In the case of Inheritance , first the base class constructor will get called , then the
derived class constructor.
class A
{
A()
{
System.out.println("A Class constructor");
}
}
class B extends A
{
B( )
{
System.out.println("B class constructor");
}
}
class ConsDemo
{
public static void main(String args[ ])
{
B ob=new B( );
}
}
Now , the statement ,
B ob=new B( );
will create the object of class B , and call the constructor, as B is the derived class
of A , so first the A class constructor will get called then the B class constructor.
output:
A class constructor
B class constructor
Method Overriding:
In the case of the method overriding the derived class will contain a method with
the same name and prototype , as defined in the base class.In such a case, the base
class method will get inherited in the derived class , but due to the same name and
layout definition in the derived class, the base class method will get overridden or
hidden.
class A
{
void show( )
{
System.out.println("A class show( )");
}
}
class B extends A
{
void show( )
{
System.out.println("B class show( )");
}
}
class MDemo
{
public static void main(String args[ ])
{
B ob=new B( );
ob.show( );
}
}
Now, using the object of class B , we can call only the show( ) method of class
B.
class A
{
void show()
{
System.out.println("class A show( )");
}
}
class B extends A
{
void show(String s)
{
System.out.println("B class show() : " +s);
}
}
class MDemo2
{
public static void main(String args[ ]) {
B ob=new B();
ob.show( );
ob.show("pace");
}
}
This is an example of method overloading.Because , in case of method overrinding
, not only the function or methods name should be same but also thier prototype
should also be same.
(i) Using super to access the data member in the base class which is being hidden
by the same name declaration in the derived class.
class A
{
int a;
}
class B extends A
{
int a;
void showAll( )
{
System.out.println("Base class a :" +super.a);
ob.shwoAll();
}
}
class A
{
void show( )
{
System.out.println("A class show( )");
}
}
class B extends A
{
void show( )
{
super.show( );
ob.show( );
}
}
class A
{
int a;
A()
{
a=0;
}
A(int i)
{
a=i;
}
void showA( )
{
System.out.println("a="+a);
}
}
class B extends A
{
int b;
B( )
{
b=0;
}
B(int i,int j)
{
// super(i);
b=j;
}
void showB( )
{
System.out.println("b="+b);
}
}
class SuperDemo3
{
public static void main(String args[ ])
{
B ob1=new B();
ob1.showA();
ob1.showB( );
B ob2=new B(5,6);
ob2.showA();
ob2.showB( );
}
}
output :
a=0
b=0
a=5
b=6
e.g.
The final data items should be initialized at the time of their declaration.
(ii) Using final to prevent inheritance
final class A
{
: : : :
: : : :
}
class B extends A
{
: : : :
: : : :
}
class A
{
final void show( )
{
: : : :
}
}
class B extends A
{
void show( )
{
: : : :
:: : : :
}
}