Multilevel inheritance – JAVA

When a class is derived from another derived class is called multilevel inheritance. It is implemented by defining at least three classes. In multilevel inheritance, there is one base class and the remaining two or more are derived classes.

Syntax:

class base_class_name

{

……………

……………

}

class derived_class_name1 extends base_class_name

{

……………

……………

}

class derived_class_name2 extends derived_class_name1

{

…………….

…………….

}

 

//PROGRAM TO IMPLEMENT MULTILEVEL INHERITANCE

class Person

{

String name;

int age;

void getdata(String a,int b)

{

name=a;

age=b;

}

}

class Teacher extends Person

{

String qualification;

float salary;

void getqual(String s)

{

qualification =s;

}

void getsal(float a)

{

salary=a;

}

 

void display( )

{

System.out.println(“Name of teacher=”+name);

System.out.println(“Age of teacher=”+age);

System.out.println(“Qualification of teacher=”+qualification);

System.out.println(“Salary of teacher=”+salary);

}

}

class guestTeacher extends Teacher

{

float ppl;  //pay per lecture

void getppl(float a)

{

ppl=a;

}

void display( )

{

System.out.println(“Name of teacher=”+name);

System.out.println(“Age of teacher=”+age);

System.out.println(“Qualification of teacher=”+qualification);

System.out.println(“Payment per lecture=”+ppl);

}

}

class testInheritance

{

public static void main(String args[ ])

{

Teacher teach=new Teacher( );

teach.getdata(“Ajay”,50);

teach.getqual(“Msc”);

teach.getsal(1200);

teach.display( );

guestTeacher gteach=new guestTeacher( );

gteach.getdata(“Aman”,30);

gteach.getqual(“Phd”);

gteach.getppl(800);

gteach.display( );

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *