Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is exact difference between Inheritance and Abstract class?

I know the fundamentals of OOP concepts[Inheritance, Abstraction, Encapsulation, Polymorphism]

We use Inheritance in case of Parent-Child relationship[Child can have all functionalities which Parent have and can add more functionality to itself too]

And we use Abstract class(In java) for a partial set of default implementations of methods in a class, which also can be implemented by simple Inheritance.

Look below example which makes my point clear.

Inheritance:

Parent class

public class Parent {

    // This method will remain same for all child classes.No need to override
    public void abc() {
        System.out.println("Parent here");
    }

    // This methods need to be overridden from child class
    public int getROI() {
        return 0;
    }
}

Child class

public class Child extends Parent{

    @Override
    public int getROI(){
        return 5;
    }

    public static void main(String[] args) {
        Child child =new Child();
        child.abc();
        System.out.println(child.getROI());
    }
}

Abstract Class:

Parent class

abstract class Parent {

    // This method will remain same for all child classes.No need to override
    public void abc() {
        System.out.println("Parent here");
    }

    // This methods need to be implemented from child class
    public abstract int getROI();
}

Child class

public class Child extends Parent{

    public int getROI(){
        return 5;
    }

    public static void main(String[] args) {
        Child child =new Child();
        child.abc();
        System.out.println(child.getROI());
    }
}

For above programs o/p will be same.

O/P:    
Parent here
5

So I think,

Inheritance: We need to override the method in child class

Abstract class: Put abstract keyword in method name and need to implement the method in child class

So Inheritance and abstract class is same regardless of abstract keyword

So we can implement abstract class using inheritance, here just method signature change classes(That's my belief).

Is there any significant difference?

like image 806
Pratik Patel Avatar asked Nov 16 '16 08:11

Pratik Patel


People also ask

What is the difference between inheritance and class?

Whereas inheritance derives one class from another, composition defines a class as the sum of its parts. Classes and objects created through inheritance are tightly coupled because changing the parent or superclass in an inheritance relationship risks breaking your code.

Is inheritance possible in abstract class?

If a class is declared abstract, it cannot be instantiated. To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it. If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.

What is diff between interface and abstract class?

Abstract Class Vs. Interface: Explore the Difference between Abstract Class and Interface in Java. The Abstract class and Interface both are used to have abstraction. An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class.


Video Answer


2 Answers

Inheritance is for inheriting properties and having some of its own as well.

Abstract is to restrict from being instantiated.

Example:
Lets take Vehicle and VehiclePart. But Vehicle as such is very abstract and not complete. So we want Vehicle class abstract because we don't want to instantiate it directly. Car is more meaningful entity than Vehicle and car is a Vehicle. So car extends vehicle and it is not abstract.

abstract class Vehicle{
    String name;
}

abstract class VehiclePart{
    String name;
    Date expiry;
}

class Car extends Vehicle{
     List<VehicleParts> parts;
}

class RacingCar extends Vehicle{

}

class Gear extends VehiclePart{
   int numOfGears;
}

Inheritance: We need to override the method in child class

Nope. in the above example you can see Car is inheriting properties like name from Vehicle. Overriding is optional. Like RacingCar can override methods of Car and make it a little bit custom. But basically it is getting(inheriting) some properties from base class. Like all the basic properties of a car will in Car and not in RacingCar. RacingCar will have properties specific to it.


Abstract class: Put abstract keyword in method name and need to implement the method in child class

Nope. It is just to restrict its instantiation. Eg. We don't want to instantiate Vehicle object because there is no meaning to it. A vehicle has to be something like car, bus etc etc. It can't just be a vehicle. So we put abstract and restrict instantiation.

like image 184
prem kumar Avatar answered Oct 20 '22 03:10

prem kumar


After java 8 you can have static and default methods in Interface. So it makes the interface much similar to abstract class.

But Still abstract class is class so we can have constructor, instance variable, getter and setter to change the state of objects. These all functionalities not provided by interface .That is main difference between interface and abstract class after java 8.

like image 2
Vijay Gawas Avatar answered Oct 20 '22 02:10

Vijay Gawas