Inheritance in Java

Inheritance in Java

Java Inheritance

Inheritance is one of the most important properties of an Object-oriented programming language. Inheritance is the process by which objects of one class acquire the properties of objects of another class. Inheritance can drive one class called a derived class from another class called parent or Base class. Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chainring, giving them a lower gear ratio.


Q. State two advantages of using the concept of Inheritance in Java.

Ans:  Advantages of Inheritance as follows  It minimizes duplicity of the code in a program reuses the base class properties into the derived class.

Q. Write the Disadvantages of Inheritance.

 Ans: Disadvantages of Inheritance as follows
     1.       In Inheritance, the relationship is tightly coupled. If we change in the Parent or Base class then all the derived class will get affected also.
  • Removing or swapping out a Base class will usually break derived classes.
  • A method declared final cannot be overridden.
  • A method can only be overridden in a subclass.
  • The method must have the same name as in the parent class
  • The method must have the same parameter as in the parent class.
  • It must be an IS-A relationship.

Q. Differentiate between the keywords extends and implements.

Ans:  Extend keyword use to extend a class or it is used to Inherit a class whereas implements are used to access an interface in Java.  We can extend only one class but we can implement multiple interfaces 

Q. How many types of Inheritance are there?

Ans:
Single Inheritance: In single inheritance, one class extends one class only.
class AClass
{
}
class BClass extends AClass
{
}
Multiple Inheritance: In multiple Inheritance, one class extends multiple classes. Java does not support multiple inheritances.
Multilevel Inheritance: In multiple Inheritance, one class extends another, and then another class as given below.
class AClass
{
}
class BClass extends AClass
{
}
Class CClass extends BClass
{
}
Hierarchical Inheritance: In the hierarchical type of inheritance, one class is extended by many subclasses.
class AClass
{
}
class BClass extends AClass
{
}
class DClass extends AClass
{
}

Q. Does Java support Multiple Inheritance?

Ans. No, Java doesn't support multiple inheritances. 

Q.  Why java doesn't support multiple Inheritance?


Ans. class A {
     void display()
    {
        System.out.println("I am from Class A");
    }
}

class B {
void display()
    {
         System.out.println("I am from Class B");
    }
}

Suppose if Java allows multiple inheritances like this,

class C extends A, B {
}

A and B display() methods are inheriting to C class.

So which display() method C class will take? As A & B class display() methods are different, So here it would Facing Ambiguity.

Q. How can we override a method in inheritance?

Ans:  If a derived or subclass method has the same name as the Base class then the method of derived class will override the Base class.
Rules of Override method in Java
Example:
/**
 * Example of Inheritance
 * Superclass InheritanceExample
 * Subclass is DerivedClass
*/
public class InheritanceExample
{
    int x, y, z;
    InheritanceExample(int a,int b)
    {
        x=a;
        y=b;
    }
    void sum()
    {
        z=x+y;
    }
    void display()
    {
        System.out.println("Sum of the Base class:" + z);
    }
}
//  Subclass

/**
 *  class DerivedClass
 *  It will inherit the properties of Super class
*/
public class DerivedClass extends InheritanceExample
{
    int z,s;
    public DerivedClass(int x,int y,int z)
    {
        super(x,y);
        this.z=z;
    }

    void sum()
    {
        // Super class sum method call
        super.sum();
        s=x+y+z;
    }
    void display()
    {
        // Super class display method call
        super.display();
        System.out.println("Sum from Derived class :"+ s);
    }
    public static void main(String args[])
    {
        DerivedClass ob=new DerivedClass(10,20,30);
        ob.sum();
        ob.display();
    }
}


Q. What is Interface?

Ans:  An Interface specifies the form of its methods but does not give any implementation details. An Interface is basically a collection of methods which are public and abstract by default.
Actually, Interfaces are not a class but define as a class but class contain methods and its behavior and attribute whereas Interfaces contains behavior that a class implements. The class can inherit multiple interfaces.
It cannot contain Constants, Fields that are private, Constructors and Static members.
The basic syntax of Interfaces is as follows.
/**
 *  interface sumInterface
 *
*/
public interface sumInterface
{
    void sum();
}
Implements in the class as follows

/**
 * class InterfaceImplement here.
 *
*
 */
public class InterfaceImplement implements  sumInterface
{
    int a,b,c;
    public void sum()
    {
        a=10;
        b=20;
        c=a+b;
        System.out.println("Summation=" + c);
    }
    public static void main(String args[])
    {
        InterfaceImplement ob=new InterfaceImplement ();
        ob.sum();
    }


SHARE THIS
Previous Post
Next Post