| written 9.5 years ago by | • modified 9.5 years ago |
Similar questions
Explain multiple inheritance in Java with example.
Marks: 10 M
Year: May 2014, May 2015, Dec 2014
| written 9.5 years ago by | • modified 9.5 years ago |
Marks: 10 M
Year: May 2014, May 2015, Dec 2014
| written 9.5 years ago by |
The class can derived from another class by following the syntax:
class subclassname extends superclassname
{
//Body of the class;
}
The keyword ‘extends’ specifies that the properties of superclassname are extended to subclassname. After this the subclass will contain all the methods of super class and it will add the members of its own.

Multiple Inheritance: In this type of Inheritance, a class is derived from more than one super class. There can be more than one Super class and only one derived class. One derived class will implement properties of many Super classes. In this example, Class A extends properties of 3 Super Class, Class B, Class C and Class D. Another example can be many students belong to only one college.

Hierarchical Inheritance: In this type of Inheritance, one super class can have multiple deriving classes. There can be many classes deriving only one super Class. In this example, Class A is having 3 derived classes Class B, Class C and Class D. 3 derived classes will extend prosperities of single base class, Class A. Another example can be One Universities can have multiple colleges affiliated.


The derived class with multilevel inheritance can be declared as follows:
class A{……..};
class B extends A{….};
class C extends B{….};
The process can be extended to any number of levels.

Code showing implementation of Single Inheritance:
class Person
{
String name;
static int age;
Person()
{
name="Nikita";
age=18;
}
void display()
{
System.out.println("Employee Name: "+name);
System.out.println("Employee Age: "+age);
}
}
class Employee extends Person
{
String emp_des;
static float emp_sal;
Employee()
{
emp_des="Manager";
emp_sal=25000f;
}
void display1()
{
super.display();
System.out.println("Employee Designation:"+emp_des);
System.out.println("Employee Salary:"+emp_sal);
}
}
class Main
{
public static void main(String args[])
{
Employee E1=new Employee();
E1.display1();
}
}