Head First Java: Chapter-7 summary
Better Living in Objectville
Inheritance
When designing applications, there can be the same behaviours and characteristics. Then we need to repeat the codes.
Example:
There are several shape classes, and they have the same methods, such as rotate() and playSound(). So we can create a new class called Shape, put common methods on that shape class and inherit all the shapes.
The class that has common features is called the super (parent) class. Super class inherit from the subclasses. Here Square, Circle, Triangle, and Amoeba are sub-classes. In Java, we call it the subclass extends the superclass.
Use inheritance when one class is a more specific type of a superclass. Consider inheritance when you have behaviour (implemented code) that should be shared among multiple classes of the same general type. Use inheritance so that you can reuse code from another class if the relationship between the superclass and subclass violates the above two rules
Amoeba rotates differently. So Amoeba must need the rotate and playa sound methods, but its implementation will differ. So Those methods must again implement in the Amoeba class. That procedure is called method override
Overriding means that a subclass redefines one of its inherited methods when it needs to change or extend the behaviour of that method.
Instance variables are not overridden because they don’t need to be. They don’t define any unique behaviour so that a subclass can give an inherited instance variable any value.
Steps for implementing inheritance.
- Look for the objects that have common behaviours.
- Design the class that represents the common state and behaviours
- Decide if a subclass needs behaviours (method implementations) specific to that particular subclass type.
- Look for more opportunities to use abstraction by finding two or more subclasses that might need common behaviour.
- Finish the class hierarchy
JVM search methods with inheritance are as follows.
JVM look owest one first. It means JVM first looks at the last subclass and then steps up if so many inheritances exist. Finally, JVM reaches the parent class. While going through each class, if it can find the particular method, it will run by JVM.
IS-A and HAS-A relationships.
IS-A relationships can be identified by using questions. As an example, Lion is an Animal. So there is an IS-A relationship between Lion and Animal classes.
The inheritance IS-A relationship works in only one direction. There Is no way to access sub-class methods by the parents through inheritance. Also, subclasses can have methods that are not in superclasses.
If class B extends class A, class B IS-A class A. This is true anywhere in the inheritance tree. If class C extends class B, class C passes the IS-A test for both B and A.
BULLET POINTS
- A subclass extends a superclass.
- A subclass inherits all superclass’s public instance variables and methods but does not inherit the superclass’s private instance variables and methods.
- Inherited methods can be overridden; instance variables cannot be overridden (although they can be redefined in the subclass, that’s not the same thing, and there’s almost never a need to do it.)
- Use the IS-A test to verify that your inheritance hierarchy is valid. If X extends Y, then X IS-A Y must make sense.
- The IS-A relationship works in only one direction. A Hippo is an Animal, but not all Animals are Hippos.
- When a method is overridden in a subclass and invoked on an instance of the subclass, the overridden version of the method is called. (The lowest one wins.)
- If class B extends A and C extends B, class B IS-A class A, class C IS-A class B, and class C is also IS-A class A.
- A final key can be used to stop inheritance. (final classes can not be Inherited)
Benefits of the inheritance.
- You avoid duplicate code.
- You define a common protocol for a group of classes.
Polymorphism
Dog dog1 = new Dog();
We now create objects as reference types, and the object type is the same.
But with polymorphism, the reference and the object can be different. The reference type can be a superclass of the actual object type with polymorphism.
Polymorphic arguments and return types
You can have polymorphic arguments and return types. With polymorphism, we can write code that doesn’t have to change when introducing a new subclass type into the program.
Rules for overridden
- · Arguments must be the same, and return types must be compatible.
- The method can’t be less accessible
Method overloading
Method overloading is nothing more than having two methods with the same name but different
argument lists. Overloading lets you make multiple versions of a method with varying lists of arguments,