Mind map
Chapter 6 Inheritance Pg 151-163
Map content
Chapter 6 Inheritance Pg 151-163
makes code extensible in any OOP language
An Overview of Inheritance
- Extending a class is done by creating a new class. This creates a parent child relationship.
- This can be repeated ad infititum unless you declare the class to be final
- Inheritance allows you to add functionality that doesn't exist in the original class
- This can be repeated ad infititum unless you declare the class to be final
- Extending a class is done by creating a new class. This creates a parent child relationship.
The extends Keyword
- Extend a class using the extends keyword in a class declaration
- public class Child extends Parent {}
- All Java classes by default extend the Java.lang.Object class. B/c Object is the ultimate superclass in Java
- In Java a class can only extend one class
- Extend a class using the extends keyword in a class declaration
The is-a Relationship
- When extending a class an 'is-a' relationship is formed
- If Animal is a superclass the a subclass can be a bird
- A bird 'is-a' animal
- subclasses inherits the methods within the superclass
- If Animal is a superclass the a subclass can be a bird
- When extending a class an 'is-a' relationship is formed
Accessibility
- subclasses can access superclasses public and protected methods and fields, but not it's private methods
Method Overriding
- When extending a class the behavior of a method in a parent class can be modified in the subclass
- Done when a subclasses method has the same signature as a parent class's method
- Simply write the new methods in the sublcass
- You cannot override a final method
- Done when a subclasses method has the same signature as a parent class's method
- When extending a class the behavior of a method in a parent class can be modified in the subclass
Calling the Superclass's Constructors
- When you create a child object, all its parent classes are also instantiated
- Super represents an instance of the direct superclass of the current object
- You can explicitly call the parents constructor from a subclass's constructor using the super keyword, but super must be the first statement in the constructor
- public Sub(String s){super(s)}
- It makes sense for a child class to call its parent's constructor from its own constructor b/c an instance of a subclass must always be accompanied by an instance of each its parents
- You can explicitly call the parents constructor from a subclass's constructor using the super keyword, but super must be the first statement in the constructor
- Super represents an instance of the direct superclass of the current object
- When you create a child object, all its parent classes are also instantiated
Calling the Superclass's Hidden Members
- The super keyword can call a hidden member or an overridden method in a superclass
- super.memberName returns the specified member in the parent class
- The super keyword can call a hidden member or an overridden method in a superclass
Type Casting
- You can only cast an instance of a subclass to its parent class
- Child child = new Child(); Parent parent = child;
- downcasting is where you cast an object down the inheritance hierarcy.
- Child child = new Child; Parent parent = child; //parent point to an instance of a Child Child child2 = (Child) parent;//downcasting
- Downcasting to a subclass is only allowed if the parent class reference is already pointing to an instance of the subclass
- Child child = new Child; Parent parent = child; //parent point to an instance of a Child Child child2 = (Child) parent;//downcasting
- You can only cast an instance of a subclass to its parent class
Final Classes
- Prevents the extension of your classes
- Final may appear after or before the access modifier
- public final class pencil OR final public class pen
- Final may appear after or before the access modifier
- Prevents the extension of your classes
The instanceof Keyword
- Instanceof keyword can be used to test if an object is of a specified type
- usually part of if statement
- String s = "Hello"; if(s instanceof java.lang.String) THIS RETURNS TRUE
- usually part of if statement
- Instanceof keyword can be used to test if an object is of a specified type
Information
- Description
- Mind Map on Chapter 6 Inheritance Pg 151-163, created by Gary Jones on 03/08/2019.
- Created by
- Gary Jones
- Views
- 0
- Rating