What is exact difference between Inheritance and Abstract class?

后端 未结 5 912
心在旅途
心在旅途 2021-02-06 00:46

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

We use Inheritance in case of Parent-Child relationship[C

相关标签:
5条回答
  • 2021-02-06 01:02

    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.

    0 讨论(0)
  • 2021-02-06 01:16

    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.

    0 讨论(0)
  • 2021-02-06 01:21

    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.

    0 讨论(0)
  • 2021-02-06 01:21

    These are two different concept and selections are based on the requirements.

    Abstraction hide the implementation details and only show the functionality. It reduce the code complexity. Inheritance create a class using a properties of another class. It improve the code reusability.

    So these are two different things for different purposes.

    If this is about the implementation(coding), obviously there are differences than putting abstract keyword in the method name.

    • Can't implement method body in abstract methods in abstract class. But can implement method body in methods in parent class.
    • In inheritance, child class inherits is parents methods. Same as in abstraction also child class inherits non-abstracts methods of parent class as they are. But child class must implement all the abstract methods in parent class, otherwise child class need to be declared as abstract.
    • Can't create instance of abstract class.
    0 讨论(0)
  • 2021-02-06 01:27

    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.

    0 讨论(0)
提交回复
热议问题