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.
Answer from prem kumar on Stack OverflowInheritance 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.
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.
Videos
My daughter is in high school AP Computer Science, which is basically Java programming. Her class is online, which we now know was a mistake because she doesn't really have a teacher teaching anything. Most of her learning is done through reading an online textbook, which is not easy.
This week's concepts are:
-
inheritance
-
abstract classes
-
interfaces
She thinks she understands the ideas of these concepts, and she can read the sample code, but she doesn't think she can write her own code using these concepts. Her class has things every Wednesday, such as tests, quizzes, and coding assignments so she is trying to understand these concepts before tomorrow.
I have her coding assignment for this week, but it's really long so I'll see what specifically she needs help with. For now, if anyone can explain these 3 concepts that would be great. Thank you for any help you can provide!
That depends, if you never want to be able to instantiate the base class then make it abstract. Otherwise leave it as a normal class.
If the base class ought not to be instantiated then make it an abstract class - if the base class needs to be instantiated then don't make it abstract.
In this example it makes sense to make the base class abstract as the base class does not have any concrete meaning:
abstract class WritingImplement
{
public abstract void Write();
}
class Pencil : WritingImplement
{
public override void Write() { }
}
However in this next example you can see how the base class does have concrete meaning:
class Dog
{
public virtual void Bark() { }
}
class GoldenRetriever : Dog
{
public override void Bark() { }
}
It is all pretty subjective really - you ought to be able to make a pretty good judgment call based on the needs of your particular domain.
Inheriting from a base class is useful if you want to use the same code as the base class and extend it with extra functionality.
Vitual and abstract are related to this. You can make a virtual method with a base implementation. A descendant class can (optionally) change or add to this implementation. An abstract class is a base class that is incomplete in itself. An abstract method is declared, but has no implementation yet. A descendant class must provide an implementation. This is useful if the base class implements a flow, but a part of that flow needs to be implemented by another class. The base class needs to be able to call that part, which is where declaring an abstract method comes in sight.
Interfaces are a different story. An interface is a contract about which methods exist in a class, but they can be implemented by two completely unrelated classes. This is convenient, because you can make small interfaces for small pieces of functionality. For example, something that can be saved can implement ISavable, which just enforces the existence of the method 'Save'. Two completely different classes can implement this, allowing for instance a Save All functionality to just save everything that can be saved.
Multiple inheritance is a specific language feature, that is not available in many languages, although in many languages you can have a similar effect by using interfaces and the delegate design pattern.
it depends on your own style.
In fact, a class which inherits from an abstract class has to use ALL attributes and methods of it - it is nearly impossible to create a clean architecture with many levels of inheritance.
The main advantage of interfaces is the flexibility - you can implement much of them but do not have to change the internal structure of your class to implement them.
In most cases, it is best practice to use interfaces, except in some software patterns like compositum or strategy pattern.
But at the end it is your decision - you have to choose the type of inheritance you want in your project.
Interfaces help you to gain flexibility, abstract classes bring more cohesiveness to your architecture because they group classes that are similar together. --> you can reuse your code of the abstract classes in its subclasses
The difference between the abstract classes and interfaces in your case could allow the developer to well-define the pattern of your design with abstract class create "is a" relationship and interface create "can do" relationship.
Put it this way, if you have a Toy car and it would be not make sense to make them "is a" relationship with Minivan since you can use interface to exhibit toy car specific behaviour like water resistance tolerance.
So you can define like:
public Interface IToyCarResistance
{
int ToleranceLevel { get; }
}
public class ToyCar : Car , IToyCarResistance
{
}
The idea that the author is trying to emphasize is that designing an abstract class instead of an interface limits your ability to mix and match that class with other parts of your system later on. This is a tradeoff between an ability to share an implementation in exchange for inability to combine multiple abstract classes later on.
In my work's codebase, we very rarely us inheritance if at all in our new modern code. In our legacy system there's some inheritance which is very ugly and hard to read. To me, inheritance opens the door to spaghetti code where calling an object's method calls a superclass' implementation of it, and makes it harder to test.
I wanted to know if and when you use inheritance, and in what scenarios? Why would you use inheritance instead of interfaces + composition?