Head First Java: Chapter-8 summary
Serious Polymorphism
Interfaces and Abstract classes
Abstraction reduces complexity by hiding unnecessary implement details.
Abstract classes can not be instantiated. So if a class does not need to create an object, we can declare that class as abstract. But abstract types can be used as reference types.
Other classes are specific enough to be instantiated, called concrete classes. Objects can be made with concrete classes. When we need to create an abstract class “abstract” keyword is used. Abstract classes can keep both abstract and concrete methods.
Abstract method.
Abstract methods have no content declaration only. So abstract methods must be overridden.
If there are one or more abstract methods, calss must be declared abstract. All abstract methods must be implemented. That means we should override those methods. The first concrete class in the inheritance tree must implement all abstract methods. But if both superclass and inherited subclass are abstract, there is no need to implement all the superclass methods on the subclass. But within the first concrete class, all the superclass and subclass methods must be initiated. When creating a concrete method for the abstract method, the same method signature and a return type must be used.
Every class in Java extends class Object. That means the class object is the mother class for everything in Java. Every class you write extends the Object class. When we extend the “Lion” class with | the” Animal” class, the Animal class will be extended to the “Object” class. So class Lion will be extended with the Object class. Any class that doesn’t explicitly extend another class implicitly extends Object.
Object class
Object class is not an abstract class. It is a concrete class. It has methods. Some are declared final some are not. So several methods of object class can be overridden.
Polymorphism
Polymorphism means ‘many forms. If there are classes, and they are inherited from each other, polymorphism will come into the picture. The detailed description of polymorphism was explained in the previous chapter.