Java Inheritance. Possible Questions and Answers
Java Inheritance. Possible Questions and Answers
1. Define Inheritance.
Inheritance is the mechanism of deriving new class from old one, old class is knows as superclass and new
class is known as subclass. The subclass inherits all of its instances variables and methods defined by the superclass
and it also adds its own unique elements. Thus we can say that subclass are specialized version of superclass.
1. Reusability of code
2. Code Sharing
3. For Method Overriding (so runtime polymorphism can be achieved).
4. Consistency in using an interface
It is a class from which other classes can It is a class that inherits some or all
class Subclass-name extends Superclass-name
{
//methods and fields
}
5. Discuss briefly about different types of inheritance in java with example programs
Example 2:
class employee
{
private int eno;
private String ename;
public void setemp(int no,String name)
{
eno = no;
ename = name;
}
public void putemp()
{
System.out.println("Empno : " + eno);
System.out.println("Ename : " + ename);
}
}
class department extends employee
{
private int dno;
private String dname;
public void setdept(int no,String name)
{
dno = no;
dname = name;
}
public void putdept()
{
System.out.println("Deptno : " + dno);
System.out.println("Deptname : " + dname);
}
public static void main(String args[])
{
department d = new department();
d.setemp(100,"aaaa");
d.setdept(20,"Sales");
d.putemp();
d.putdept();
}
}
2. Multilevel Inheritance – the concept of one class extending (Or inherits) more than one base class.
Example 1:
class students
{
private int sno;
private String sname;
public void setstud(int no,String name)
{
sno = no;
sname = name;
}
public void putstud()
{
System.out.println("Student No : " + sno);
System.out.println("Student Name : " + sname);
}
}
class marks extends students
{
protected int mark1,mark2;
public void setmarks(int m1,int m2)
{
mark1 = m1;
mark2 = m2;
}
public void putmarks()
{
System.out.println("Mark1 : " + mark1);
System.out.println("Mark2 : " + mark2);
}
}
class finaltot extends marks
{
private int total;
public void calc()
{
total = mark1 + mark2;
}
public void puttotal()
{
System.out.println("Total : " + total);
}
public static void main(String args[])
{
finaltot f = new finaltot();
f.setstud(100,"Nithya");
f.setmarks(78,89);
f.calc();
f.putstud();
f.putmarks();
f.puttotal();
} }
Example 2:
3. Hierarchical inheritance
In Hierarchical inheritance one parent class will be inherited by many sub classes. As per the
below example ClassA will be inherited by ClassB, ClassC and ClassD. ClassA will be acting as a parent
class for ClassB, ClassC and ClassD.
Example 1:
c.dispA();
ClassD d = new ClassD();
d.dispD();
d.dispA();
}
6. Write a java program using class Shap and three sub class like square, Rectangle and Circle calculate are of
each using inheritance.
class Shape
{
//define a length which is public and can be used in square,
//rectangle and circle classes
class InheritanceDemo
{
public static void main(String[] args)
{
//object of child class square
Square sq = new Square();