GoConqr has a new look! Let us know what you think.

Mind map

Chapter 6 Inheritance Pg 151-163

Gary Jones English

Map content

Chapter 6 Inheritance Pg 151-163

  1. makes code extensible in any OOP language

  2. 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
  3. 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
  4. 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
  5. Accessibility

    • subclasses can access superclasses public and protected methods and fields, but not it's private methods
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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

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

Feedback

What is it about?

What do you think of the new look?

How do you like it?