- Objects
- Classes
- Inheritance
-
Programming in Java on Your Computer - Classes and Objects in BlueJ
- Trying out Code in BlueJ
- The Code for Creating and Using Objects
- Source Code and Methods
- Accessors and Mutators
- Constructor Code
- Scope
- Inheritance
-
BlueJ Review - Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
Refactoring Code with Inheritance
Optional NodePremium Content - Free Preview
If you examine the Circle, Triangle and Square Classes, you'll notice that there's a large amount of code overlap between them. This usually means that you should refactor your code to get rid of the duplication. In this case, you could use inheritance so that each shape can inherit from a superclass called Shape
. To see if Inheritance is a good idea, you can use the IS-A test:
- Square Is a Shape?
- Circle Is a Shape?
- Triangle Is a Shape?
Hopefully you answered yes to all of these questions. That means it would make sense to create a new superclass Shape
and make Square, Circle, and Triangle inherit from it.
Refactoring Square
- Create a new Class called Shape.
- Look at the Square Class and see which instance variables it has in common with the other shapes and which is uniquely Square's.
- Look at the Square Class and examine what methods it has in common with other shapes and what is uniquely Square.
- Cut and paste all of the non-unique methods and instance variables from Square to the new Shape class.
- Make Square inherit from Shape by using the
extends
keyword.
Then you need to adjust the code to get the new, lean Square class to work:
- variable access - The private variables in Shape will not be available to Shape's subclasses. Remove the private modifier from them.
- constructor - Most of the constructor can be moved to Shape also. However, you will need to set
size
in Square. You can callsuper
to explicitly set everything else.
abstract keyword and Polymorphism
abstract
What would an instance of the superclass Shape look like? It would be hard to draw a Shape without having a specific one in mind. This means that it would make sense to make the Shape class abstract
. An abstract
class is a class that cannot have instances of it created. It exists for its subclasses to inherit from and create their own instances. You can declare a class abstract
by adding the keyword before class
:
End of Free Content Preview. Please Sign in or Sign up to buy premium content.
Comments
Shane
Nov 3, 6:09 AMI can't do this last task- I've tried to create an array Shape []shapes=new Shape[3];
and then can't instantiate Shape() as it's abstract.
I tried shapes[0]= Circle(); but this doesn't affect the whole Class.
I'd really to understand this. A hint or a cheat would be appreciated.
Learneroo
Nov 3, 11:14 PM@Shane, I can't see your code (since it's offline), but did you use the correct code when creating the Circle?
shapes[0] = new Circle();
If all the shapes are correctly in one Shape array, you can create a method that will callchangeColor
of each Shape:public void changeShapeColor(String color) {
for(int i=0; i<shapes.length; i++){
shapes[i].changeColor(color);
}
}
David
Mar 27, 8:21 PMDear Admin,
For the Refactoring Square exercise, moveVertical() method should be moved to the shape super class i believe based on the IS-A test.
The problem is that when i try to compile the Shape class i get>> cannot find symbol method draw().
Of course this would be expected because it is not declared in the Super Class, but since it should be declared in Square which is a sub class i cannot call it through super.
Any hints would be greatly appreciated.
Best Regards.
David
Mar 27, 10:36 PMMeh -_-! Sorry Admin.... I had not read about the polymorphic methods... so use abstract for the draw method...
James
Apr 4, 9:19 AMHow do you do the refactoring task as when you move funcitons such as makeVisible it calls the draw function, which there isnt one in the Shapes class? If the draw function is put in the shapes class it will only draw that shape as draw is called within shapes not the shape (circle). Thanks
James
Apr 4, 9:40 AMsolved read the next part of the node
Lukas Dancak
Nov 27, 11:28 AMI did must change the status of draw() method in square from "private" to "public", because of compilation was not possible. Is this correct solution ?
Learneroo
Nov 27, 8:34 PMdraw
can be private in Square on it's own. But it can't stay private when you refactor to inherit from Shape.