As you’ve seen in the UML class relationships, there is the ability to define a class, and a subclass. The subclass will get all of the properties and methods of the parent class, and can then add additional properties and methods that are not available in the parent.
Consider two classes, a Shape2D and a Shape3D. Much, if not all, of what is in Shape2D can and will be used in Shape3D. However Shape3D would have properties and methods which do not exist in Shape2D. Consider these properties and methods for each to see what I mean.
Shape2D properties and methods:
- xCoord
- yCoord
- area()
- perimeter()
- distance()
Shape3D properties and methods:
- xCoord
- yCoord
- zCoord
- volume()
- distance()
Inheritance
Here it becomes more obvious about how there is a lot of overlap.
With inheritance, we get to use any attribute that was in our parent class. Additionally, we get to use any method as well in the parent as well. We don’t have to define them, or write them, they come automatically.
To inherit something from a parent class, we use the extends
keyword like you see below:
public class Shape3D extends Shape2D { ... }
That’s a pretty simple process, and it does a “ton” of work for you making it easy to import attributes and methods.
Overriding Methods
If a child and a parent have the same method, and the child implements it and overrides the parents method for the child. So the parent would still have their own method, but the child’s would be different – being the new content.
A common example of this is the toString()
method. toString is defined with the class Object, which all classes extend. It is used when you pass an object to a stream operator by default, such as System.out.println(obj);
. The toString method provides some basic functionality, but nothing too useful either in many ways.
Because of that, we often want to override the method so we can print something more useful than the class name and memory reference.
To override a method, you just need to create the same method name with the same return types and parameters as shown below.
// overridding the toString method
String toString() {
return "My shape at location " + /*.... snip ....*/;
}
Method Overloading
Now in some cases, like with distance, the method you would think would need to be overridden because the formula for finding distance in 2D is different from 3D distance.
However, you’d probably actually overload the method, since you would be passing a different datatype in as a 3D object vs a 2D object. This way you could still use a 2D distance method if necessary since the 3D object would extend the 2D object.
// within Shape2D
public double distance(Shape 2D) {
// calculate and return the 2D distance
}
// within Shape3D
public double distance(Shape 3D) {
// calculate and return the 3D distance
// this is overloaded, as you can still call distance and pass it a 2D object
}
Abstract Methods
In some case, you need to make a parent’s method abstract, because you don’t know how it will be implemented. Think of different types of 2D shapes: Circle, Square, Rectangle, etc. Each of these would have a perimeter and area method, but the way you calculate those values will be different for each of the shapes.
Look at the example below for what you see within Shape2D and Square classes.
// Shape2D
public abstract double area();
// Square
public double area() {
return this.side * this.side;
}
Notice how the method within Shape2D does not have a body. This is because you cannot call the method area as part of the Shape2D method.
If we define that method as abstract, then any child would need to override the abstract method if it needs to be instantiated. If a class has any abstract methods, you cannot create an instance (object) of the class. Thus, one abstract method makes for an abstract class, thus you might see something like what is below.
public abstract class Shape2D {
// declare fields
// declare nonabstract methods
// declare abstract methods
abstract void area();
}
Inheritance in Java was originally found on Access 2 Learn
One Comment
Comments are closed.