We have introduced the 4 pillars of object orientation, and how they constitute the fundamentals of object-oriented design. This module presents more details on how to make use of the concept of abstraction through Java constructs called interfaces and abstract classes, the syntax and general principles of using them in our programs.
Interfaces
Abstract classes
More details on how abstract classes are designed in Java can be found on the Oracle Abstract classes tutorial available on this link.
Exercise
In this exercise, we will practice the principles of object-oriented design using abstract classes. The exercise is to implement objects that represent different animals, such as dogs and chameleons. Each object should have some common properties that indicate they belong to the same ancestor class, such that they are all of animal type, but also have some specialized properties and methods that define their animal type. We can implement this program by defining a class called Animal class that represents all animals. This call can be abstract, as long as no Animal objects are ever created. The Dog and Chameleon classes can implement the methods of Animal class and actually create objects from each animal type. Please design and implement a Java program that has an abstract class called Animal, with properties : numLegs, thermicCondition and breedingType and two methods called eat() and sleep(). Then, implement two concrete classes that are subclasses of the Animal class: Dog and Chameleon, which should implement the Animal class methods such that they are in line with the specific animal they represent. The Dog class should additionally have the properties name and breed, and methods bark() and run(). The Chameleon class, on the other hand, should have the new properties name and age, and the additional method changecolor(). Please also implement the constructor method for each class such that the properties of the Animal class can be set to the predefined values for each type of animal object.
Solution
We can start with the implementation of the Animal class as below.
The Dog class will add the new properties and methods as however we would like to define the distinction. Here for illustration purposes, we use print methods to define the procedure in each method.
Similarly, the Chameleon class will add its own version of the properties and methods inherited from the Animal class.
If we were to create instances from the Dog class, we can use the constructor method we defined earlier, and call different methods specific to the Dog objects.
Chameleon class will also provide a concrete medium to the Animal class to be able to create specific instances from it, belonging to the Chameleon type defined earlier.
Note that as long as the Animal class stays abstract, we will never be able to create instances from it. All extensions, or subclasses, of the abstract Animal class will need to implement all of its abstract methods and constructors if there will be instances created from them.