Inheritance and Polymorphism,
Head First Java Edition ii Chapter vii.
*In this chapter, I go over more specifics about inheritance and introduce the ideas of polymorphism.
*Let’s have a look at inheritance in greater detail.
Inheritance,
*When you design with inheritance, you put common code in a class and then alert other more specific classes that the common (more abstract) class is their superclass. When one class inherits from another, the subclass inherits from the superclass. For subclasses that inherit from a superclass, the key term “extends” is used.
*A subclass inherits from a superclass member. Members of the super class include methods and instance variables. Subclasses inherit the attributes of Methods and the variables of the superclass.
*Let’s look at an inheritance example. SuperHero is a super class, while the others are subclasses. Sub classes can utilise the super class’s methods and variables (FriedEggMan and PantherMan).
*These are the factors to consider when creating an inheritance.
- Look for objects with similar characteristics and actions.
- Create a class that encapsulates the shared state and behavior.
- Determine whether or not a subclass requires behaviors that are exclusive to that type of subclass.
- Identify two or more subclasses that may require similar functionality to find more abstraction opportunities.
Using IS-A and HAS-A,
*You may rapidly determine whether a superclass can be extended by a subclass by using the ‘IS A’ command. IS-A can only work in one direction.
*Class B IS-A class A if it extends class A. This is true at any point in the inheritance family tree. Class C satisfies the IS-A test for both B and A if it extends class B.
*‘HAS-A’ has nothing to do with ancestry. The Has-A relationship simply states that an instance of one class refers to the occurrence of another class or another instance of a comparable class.
Polymorphism,
*You can refer to a subclass object utilizing polymorphism by using a reference designated as the super type.
*To see how polymorphism works, consider the following example.
*Look at the way we normally declare a reference and create an Dog object of Dog class. This Dog class extends form Animal class (super class),
Dog myDog = new Dog(); // normal object creation from dog class
with Polymorphism,
Animal myDog =new Dog();
*The reference type in polymorphism can be a superclass of the actual object type.
*If we write code using polymorphic arguments and declare method parameters as superclass types, we can send in any subclass object at runtime.
What are rules of the overriding,
- Arguments must be the same, and return types must be compatible.
- The method can’t be less accessible.
Overloading Methods,
*An overloaded method is simply another version of the same method with a different name. It has nothing to do with inheritance or polymorphism. A method that has been overridden is not the same as one that has been overloaded.
References,
[1]Sierra, Kathy, and Bert Bates. “Head First Java™ Second Edition.” (2021)