last weekend i read some stuff about interfaces, abstract classes and design principles. At the end i got a bit confused and i tried to build an example of what i learned (or thought i had learned).
Here is my example: The case would be to model a class that holds informations about trees.
First of all i would make an interface:
public interface Tree{
public void grow();
}
The interface holds all methods that should be implemented by the concrete trees. So far so good but such a tree needs some attributes (variables) that are shared over all tree families. For that purpose i would use a abstract class:
public abstract class AbstractTree implements Tree {
private String barColor;
private int maxHeight;
private boolean isEvergreen;
}
Is this the right way or am i not able to make a kind of contract about attributes (variables) that should be in the other classes?
After the attribute part is done i would like to have 3 type of trees.
So each of these tree "tpyes" can have individual variables.
public class OakTreeImpl extends AbstractTree{
private String barColor;
private int maxHeight;
private boolean isEvergreen;
private String foo;
@Override
public void grow() {
}
}
Does this approach sound right in an object-oriented design principles way or am i totally wrong with it?
In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
Java Abstract class can implement interfaces without even providing the implementation of interface methods. Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation.
Abstract classes can include abstract methods. Any class that extends a class with an abstract method must implement that method. For example, our Mammal class includes an abstract speak() method. Any class that extends Mammal must implement the speak method, and that implementation must have the same signature.
An interface consists only of abstract methods. A class will implement the interface and define these abstract methods as per the required functionality. Unlike extends, any class can implement multiple interfaces.
An interface can extend other interfaces, just as a class subclass or extend another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces. The interface declaration includes a comma-separated list of all the interfaces that it extends.
A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.
This does work, however it does not make much sense, since the interface is totally obsolete in this case.
You should add the grow method to your AbstractTree
like this:
public abstract class AbstractTree{
protected String barColor;
protected int maxHeight;
protected boolean isEvergreen;
public abstract void grow();
}
Using an interface would make sense, if you wanted to have different kinds of plants that should all be able to grow for instance.
interface Plant{
void grow();
}
abstract class Tree implements Plant{
void grow(){ /* do sth */ }
}
abstract class Flower implements Plant{
void grow(){ /* do sth totally different */
}
The purpose of an interface is to provide the same method in multiple classes with different implementations, whereas an abstract class provides methods and attributes that are shared in all of their child classes.
If a method in an abstract class is also abstract, every child class must implement it themselves.
I would rather mark the instance variables as protected
.
Because all protected members of a super class is accessible to child classes. If and only if, parent and child classes are in the same package
public abstract class AbstractTree implements Tree {
protected String barColor;
protected int maxHeight;
protected boolean isEvergreen;
}
public class OakTreeImpl extends AbstractTree{
// I can access barColor, maxHeight, isEvergreen in this class
@Override
public void grow() {
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With