0% found this document useful (0 votes)
15 views19 pages

Inhertiance Topic

The document explains the use of the static keyword in Java for defining static data items, static blocks, and static functions, along with examples. It also covers inheritance types such as single, multilevel, hierarchical, and hybrid inheritance, along with the concepts of method overriding and the use of the super keyword. Additionally, it discusses the final keyword's applications in defining constants, preventing inheritance, and preventing method overriding.

Uploaded by

Pace Infotech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views19 pages

Inhertiance Topic

The document explains the use of the static keyword in Java for defining static data items, static blocks, and static functions, along with examples. It also covers inheritance types such as single, multilevel, hierarchical, and hybrid inheritance, along with the concepts of method overriding and the use of the super keyword. Additionally, it discusses the final keyword's applications in defining constants, preventing inheritance, and preventing method overriding.

Uploaded by

Pace Infotech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

static keyword:

The static keyword in Java is used for three different purposes,

(i) defining the static data items


(ii) defining the static block
(iii) defining the static function

Consider the following program,

class StaticDemo
{
static int a=10;
static int b;

static void show( )


{
System.out.println("a="+a);
System.out.println("b="+b);
}

static
{
System.out.println("In the static block");
b=90;
}

public static void main(String args[ ])


{
System.out.println("In the main( ) method");
show( );
}
}

(a) static int a=10;


static int b;

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.

Sample ob1=new Sample();


Sample ob2=new Sample();

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.

Some its limitations are,

(i) It can access only static data items.

(ii) It can calll only static functions.


(c) static void show( )
{
:::::
}

it is a static function .
Some its limitations are ,

(i) It can access only static data items.

(ii) It can calll only static functions.

output :
In static block
In main( ) method
a=10
b=90

Consider the following program,

class Static2
{
int a;

static int b;

void setAll(int i,int j)


{
a=i;
b=j;
}

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( );

Static2 ob2=new Static2( );

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 ,

class DerivedClassName extends BaseClassName


{
body of the Derived Class
}

The extend keyword is used for creating the derived class.

(a) Single Inheritance :

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;

public void setA(int i)


{
a=i;
}

public void showA( )


{
System.out.println("a="+a);
}
}
class B extends A
{
private int b;

public void setB(int j)


{
b=j;
}

public void showB( )


{
System.out.println("b="+b);
}
}
class SingleDemo
{
public static void main(String args[ ])
{
B ob=new B( );

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

(ii) These members will not get inherited.

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,

(i) These members are directly accessible by the object of a class.

e.g.
ob.setA(5) ; is valid

(ii) These members will get inherited.

That is, they can be used in the derived class or by the object of the derived class.

B ob=new B();

ob.showA();

(b) Multilevel Inheritance

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;

public void setA(int i)


{
a=i;
}

public void showA( )


{
System.out.println("a="+a);
}
}
class B extends A
{
private int b;

public void setB(int j)


{
b=j;
}

public void showB( )


{
System.out.println("b="+b);
}
}
class C extends B
{
private int c;

public void setC(int k)


{
c=k;
}

public void showC( )


{
System.out.println("c="+c);
}
}

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();

}
}

(c) Hierarchical Inheritance :


In this type of inheritance , we create multiple derived classes on the basis of the
single base class.
class A
{
private int a;

public void setA(int i)


{
a=i;
}

public void showA( )


{
System.out.println("a="+a);
}
}
class B extends A
{
private int b;

public void setB(int j)


{
b=j;
}

public void showB( )


{
System.out.println("b="+b);
}
}
class C extends A
{
private int c;

public void setC(int k)


{
c=k;
}

public void showC( )


{
System.out.println("c="+c);
}
}

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.

Calling of constructors in 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.

Consider the following program ,

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.

Method Overriding V/S Method Overloading

Consider the following code ,

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.

Using super keyword

super keyword is used to refer to the immediate base class.

It is used in java for three different purposes ,

(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 setAll(int i,int j )


{

super.a=i; /*A class a*/

a=j; /* B class a*/


}

void showAll( )
{
System.out.println("Base class a :" +super.a);

System.out.println("Derived class a:"+a);


}
}
class SuperDemo1
{
public static void main(Strign args[ ])
{
B ob=new B();
ob.setAll(7,8);

ob.shwoAll();
}
}

(ii) Using super to resolve method overriding

class A
{
void show( )
{
System.out.println("A class show( )");
}
}
class B extends A
{
void show( )
{
super.show( );

System.out.println("B class show( )");


}
}
class SuperDemo2
{
public static void main(String args[ ])
{
B ob=new B( );

ob.show( );
}
}

(iii) Using super to call the base class parameterized constructor.

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

Using final keyword

(i) Using final to define a constant

The general form is ,

final datatype dataitem=value;

e.g.

final int i=5;

The final data items should be initialized at the time of their declaration.
(ii) Using final to prevent inheritance

If the base class is defined as final , it cannot be inherited.

final class A
{
: : : :
: : : :
}
class B extends A
{
: : : :
: : : :
}

Now , this code will generate an error.

(iii) Using final to prevent method overriding

In the base class method is defined as final then it cannot be overridden.

class A
{
final void show( )
{
: : : :
}
}
class B extends A
{
void show( )
{
: : : :
:: : : :
}
}

This code will generate an error.

You might also like