Mindmap
Inheritance
Inhalt der Mindmap
Inheritance
An Overview of Inheritance
- The process of extending a class in OOP is called inheritance
- Inheritance gives you the opportunity to add some functionality that does not exist in the original class.
- It also gives you the chance to change the behavior of the existing class to better suit your needs
- The extends Keyword
- You extend a class by using the 'extends' keyword in a class declaration, after the class name and before the parent class.
- Ex. Listing 6.1 presents a class named Parent and another class names Child which extends Parent. Object is the ultimate superclass in Java. Parent by default is a subclass of Object.
- You extend a class by using the 'extends' keyword in a class declaration, after the class name and before the parent class.
- The is-a Relationship
- There is a special relationship that is formed when you create a new class by inheritance. The subclass and the superclass has an “is-a” relationship.
- For example, Animal is a class that represents animals. There are many types of animals, including birds, fish, and dogs, so you can create subclasses of Animal that represent specific types of animals.
- The is-a relationship does not go the other way, however. An animal is not necessarily a bird or a dog.
- A consequence of the is-a relationship is that it is legal to assign an instance of a subclass to a reference variable of the parent type.
Accessibility
- From within a subclass you can access its superclass’s public and protected methods and fields, but not the superclass’s private methods.
- If the subclass and the superclass are in the same package, you can also access the superclass’s default methods and fields.
- P has three methods, one public, one protected, and one with the default access level.
- However, it does not mean you can expose P ’s non-public methods through its subclass.
- protectedMethod is P ’s protected method. It is not accessible from outside P , except from a subclass.
Method Overriding
- When you extends a class, you can change the behavior of a method in the parent class.
- This is called method overriding, and this happens when you write in a subclass a method that has the same signature as a method in the parent class.
- You override a method to change its behavior. To override a method, you simply have to write the new method in the subclass, without having to change anything in the parent class.
- The Box class extends the java.lang.Object class. It is an implicit extension since the extends keyword is not used. Box overrides the public toString method and the protected clone method.
- You cannot override a final method. To make a method final, use the final keyword in the method declaration. For example: public final java.lang.String toUpperCase(java.lang.String s )
Calling the Superclass's Constructors
- A subclass is just like an ordinary class, you use the new keyword to create an instance of it. If you do not explicitly write a constructor in your subclass, the compiler will implicitly add a no-argument (no-arg) constructor.
- In the parent class, the constructor also calls the constructor of its direct parent class. This process repeats itself until the constructor of the java.lang.Object class is reached. In other words, when you create a child object, all its parent classes are also instantiated.
- You can explicitly call the parent’s constructor from a subclass’s constructor by using the super keyword, but super must be the first statement in the constructor.
- Using the super keyword is handy if you want another constructor in the superclass to be invoked.
- It actually makes sense for a child class to call its parent’s constructor from its own constructor because an instance of a subclass must always be accompanied by an instance of each of its parents. This way, calls to a method that is not overridden in a child class will be passed to its parent until the first in the hierarchy is found.
Calling the Superclass's Hidden Members
- The super keyword has another purpose in life. It can be used to call a hidden member or an overridden method in a superclass.
- You can access any member in the superclass that is visible from the subclass. For example, Listing 6.8 shows two classes that have a parent-child relationship: Tool and Pencil
Type Casting
- You can cast an object to another type. The rule is, you can only cast an instance of a subclass to its parent class. Casting an object to a parent class is called upcasting. Here is an example, assuming that Child is a subclass of Parent .
- To upcast a Child object, all you need to do is assign the object to a reference variable of type Parent . Note that the parent reference variable cannot access the members that are only available in Child
- Downcasting to a subclass is only allowed if the parent class reference is already pointing to an instance of the subclass. The following will generate a compile error.
Final Classes
- You can prevent others from extending your class by making it final using the keyword final in the class declaration. final may appear after or before the access modifier. For example: public final class final public class Pencil
- Even though making a class final makes your code slightly faster, the difference is too insignificant to notice. Design consideration, and not speed, should be the reason you make a class final.
The Instance of Keyword
- The instanceof keyword can be used to test if an object is of a specified type. It is normally used in an if statement and its syntax is. if ( objectReference instanceof type )
- However, applying instanceof on a null reference variable returns false . For example, the following if statement returns false . String s = null; if (s instanceof java.lang.String)
- Also, since a subclass “is a” type of its superclass, the following if statement, where Child is a subclass of Parent , returns true . Child child = new Child(); if (child instanceof Parent) // evaluates to true
Informationen
- Beschreibung
- Mindmap am Inheritance, erstellt von Anabella Vazquez am 21/11/2019.
- Erstellt von
- Anabella Vazquez
- Aufrufe
- 0
- Bewertung