What is exact difference between Inheritance and Abstract class?

29,781

Solution 1

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.

Solution 2

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.

Solution 3

With inheritance you don't need to override a method. Without overriding getROI in Child you could still call new Child().getROI() and get 0 as response.

If on the other hand a method is abstract, it will need to be implemented by the child as there is no default implementation.

Solution 4

An abstract class means you can't instantiate it directly.

new Parent()

is not allowed.

An abstract method will need to be implemented in an extended class.

Share:
29,781
Pratik Patel
Author by

Pratik Patel

An Automation cum Devops engineer working in Computer Industry for more than 5 years and having 4+ years of experience in Automation Testing. I find tremendous interest in learning new computer and programming technologies(Automation especially) and also like to read tech blogs, news and stay updated with the latest trends in the technology world. Have vast knowledge in verticals of Programmatic Advertising &amp; Marketing, Media and Entertainment, Banking &amp; Finance, E-Commerce. When I am not working, I am usually with my Computer playing video games, watching the tech-series, I also love to cook and travel wherever I can! I love to meeting new peoples and learning new things, so please feel free to say hello and share your a with me. TECHNICAL SKILLS: Programming Languages: Java, Python, JavaScript/NodeJS Automation Technologies: Selenium, Appium, Cypress, Espresso, XCUITest, Docker, Jenkins, Gradle, Maven NPM libraries: wd/webdriver.io(Appium), protractor/selenium-webdriver(Selenium), chai, mocha/jasmine(Test framework), sync-request(Synchronous request for API testing), cypress(web-automation testing) Automation Cloud-based platforms: Saucelabs, Kobiton, Browserstack, AWS Device Farm Automation Frameworks/Tools: Se-Lion, Robot, Galen(UI), Redwood HQ, Katalon Studio, Appium Studio, Ranorex Machine learning and Data Scrapping Libraries: BeautifulSoup4, Scrappy, NumPy, SciPy, Scikit-learn, Tensorflow Virtualization/Cloud Technologies: AWS EC2, Google Compute Engine, VMWare ESX Project Management: JIRA, Slack, Trello, AirTable, Confluence, Redmine.

Updated on July 05, 2022

Comments

  • Pratik Patel
    Pratik Patel almost 2 years

    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?