Description
Based on the tutorial of “2020S-Java-A” designed by teaching group in SUSTech
Objective
Learn inheritance.
Learn protected keyword.
Part 1: Inheritance
1. Before Exercise
1.1 Download resource
download source code of inheritance in sakai website. Place all java files at the same location.
1.2 Source code analysis
From the source code, it is observed that the two classes: Circle and Rectangle have a lot of common fields, e.g., screenSize , x , y and ShapeColor , and a lot of similar methods. It is a good time to practice inheritance by refactoring the code.
The idea of inheritance is simple but powerful: When you want to create a new class and there is an existing class which includes some of the code that you want, you can extend your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the subclass must invoke one of the constructors in its superclass.
(https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html )
We found that, the attributes x, y, color and screenSize are both in Circle and Rectangle, then for those common attributes are more appropriated to be extracted into a super class named Shape, from which the subclass can use all attributes and methods.
1.3 Create a super class
Create a class Shape , which contains following members:
Adding attributes
Refactoring CircleColor to ShapeColor
1. Right click the class name CircleColor
2. Click Refactor -> Rename
3. Then Rename it to ShapeColor
Adding constructors with two parameter x and y
1.4 Modify subclass
Remove those methods and attributes which can be inherited from class Shape.
Now class Circle only needs to define two specific attributes: radius and DEFAULT_RADIUS.
Modify the constructor of class Circle as follows and use super () .
this serves as the current object, while super serves as the only supper class for current object.
1.5 Access the instance fields and static fields of super class in a subclass.
We will find that some errors occur in other methods, for example:
Change Circle.screenSize to Shape.getScreenSize() since screenSize is a private static field.
Change this.x to super.getX() since x is a private field of supper class, and so on.
Change other methods in the same way.
1.6 protected keyword
We can find that Circle is inconvenient to access the private attributes of superclass, so we can consider making these frequent-used attributes accessible to subclass. Protected can help us.
Change x, y and color from private to protected.
Then we change the isInBoundary() back to the original one except Shape.getScreenSize() , it can work well .
Change other methods in the same way.
The access right of each access modifier is shown as follows:
private default protected public
Same package same class ok ok ok ok
Same package other classes ok ok ok
Other packages Other classes. Inheritance ok ok
Other packages Other classes. No inheritance ok
2. Exercise
Exercise 1
Now, modify the given class Rectangle to make it inherits from class Shape
Make Rectangle extends Shape. Modify the constructors of Rectangle Modify other methods of Rectangle. Modify toString() method.
Run the following ShapeTest to test your modifications.
Output
Circle{radius=0.1, x=1.0, y=1.0, color=GRAY}
Circle{radius=0.1, x=1.0, y=1.0, color=GREEN}
Circle{radius=0.1, x=0.5, y=2.0, color=RED}
Rectangle{width=0.5, height=0.5 x=0.0, y=0.0, color=GRAY}
Rectangle{width=0.5, height=0.5 x=0.0, y=0.0, color=GREEN}
Rectangle{width=0.5, height=0.5 x=2.0, y=1.0, color=RED}
Reviews
There are no reviews yet.