You are on page 1of 8

Inheritance

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.

2. What are the benefits / advantages of java’s inheritance?

1. Reusability of code
2. Code Sharing
3. For Method Overriding (so runtime polymorphism can be achieved).
4. Consistency in using an interface

3. What are the difference between super Class and subclass.

Superclass(Base Class) Subclass(Child Class)

It is a class from which other classes can It is a class that inherits some or all

be derived. members from superclass.

4. Give the Syntax of Java Inheritance

class Subclass-name extends Superclass-name  
{  
   //methods and fields  
}  

5. Discuss briefly about different types of inheritance in java with example programs

There are 3 types of inheritance in java.


1) Single Inheritance
When a class extends another one class only then we  call it a single inheritance. The below flow diagram
shows that class B extends only one class which is A. Here A is a parent class of B and B would be  a child class of
A.

Example 1: Write any one for Single Inheritance

public class Shape


{
int length;
int breadth;
}
public class Rectangle extends Shape
{
int area;
public void calcualteArea()
{
area = length*breadth;
}
public static void main(String args[])
{
Rectangle r = new Rectangle();
//Assigning values to Shape class attributes
r.length = 10;
r.breadth = 20;
//Calculate the area
r.calcualteArea();
System.out.println("The Area of rectangle of length \""
+r.length+"\" and breadth \""+r.breadth+"\" is \""+r.area+"\"");
}
}
Output :

The Area of rectangle of length "10" and breadth "20" is "200"

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:

multilevel with constructor example java program

//multilevel with constructor


 class students1
{
        private int sno;
        private String sname;
        students1(int no,String name)
        {
                sno = no;
                sname = name;
        }
        public void dispstud()
        {
                System.out.println("Student No : " + sno);
                System.out.println("Student Name : " + sname);
        }
}
class marks1 extends students1
{
        protected int mark1,mark2;
        marks1(int n1,int n2,int sno,String sname)
        {
                super(sno,sname);
                mark1 = n1;
                mark2 = n2;
        }
        public void dispmarks()
        {
                System.out.println("Mark1 : " + mark1);
                System.out.println("Mark2 : " + mark2);
        }
}
class finaltot1 extends marks1
{
        private int total;
        finaltot1(int n1,int n2,int no,String name)
        {
                super(n1,n2,no,name);
                total = mark1 + mark2;
        }
        public void disptotal()
        {
                System.out.println("Total : " + total);
        }
        public static void main(String args[])
        {
                finaltot1 f = new finaltot1(90,89,100,"Kirthika");
                f.dispstud();
                f.dispmarks();
                f.disptotal();
        }
}

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:

public class ClassA


{

public void dispA()


{
System.out.println("disp() method of ClassA");
}
}

public class ClassB extends ClassA


{

public void dispB()


{
System.out.println("disp() method of ClassB");
}
}
public class ClassC extends ClassA
{
public void dispC()
{
System.out.println("disp() method of ClassC");
}
}

public class ClassD extends ClassA


{
public void dispD()
{
System.out.println("disp() method of ClassD");
}
}

public class HierarchicalInheritanceTest


{
public static void main(String args[])
{
ClassB b = new ClassB();
b.dispB();
b.dispA();
ClassC c = new ClassC();

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

public int length = 10;


}
//inherit the properties of Shape class with the use of extends keyword

class Square extends Shape


{
void area()
{
//calculate area of square

int area = length * length;


//print it on the screen
System.out.println("Area of square : "+area);
}
}
class Rectangle extends Shape
{
void area()
{
//define a breadth
int breadth = 7;
//calculate area of rectangle
int area = length * breadth;
//print on the screen
System.out.println("Area of rectangle : "+area);
}
}

class Circle extends Shape


{
void area()
{
//calculate area of circle using length of the shape class as radius

float area = 3.14f * length * length;

//print on the screen


System.out.println("Area of circle : "+area);
}
}

//make a entry class which contains main method

class InheritanceDemo
{
public static void main(String[] args)
{
//object of child class square
Square sq = new Square();

//object of child class rectangle


Rectangle rec = new Rectangle();

//object of child class circle


Circle cir = new Circle();

//call the area methods of all child class to


//get the area of different objects
sq.area();
rec.area();
cir.area();
}
}

You might also like