PROGRAMMING

MINTE9
REMEMBERS




Last update:   23-07-2021

Inheritance

p181 Subclasses extends a super class. Superclass defines what subclasses have in common, then abstract those features. Don't use inheritance just to reuse code.

Abstract

p200 How you decide if a class should extend another class? Is a Dog an Animal? YES Is a Dog a Cat? NO Has Dog a height? YES
 RunCode
class LearningApp {

    public static void main(String[] args) {

        Dog d = new Dog();
        d.setName("Max");
        d.setAction("barking");
        d.showAction();  // The Dog Max is barking.

        Bird b = new Bird();
        b.setAction("flying");
        b.showAction(); // The Bird null is flying
    }
}

abstract class Animal {

    public String name; // null
    public String action; // null

    public void setName(String name) { 
        this.name = name; 
    }

    public void setAction(String action) { 
        this.action = action; 
    }

    public abstract void showAction(); // abstract
}

class Dog extends Animal {

    @Override
    public void showAction() {
        System.out.println(
            "The Dog " + this.name + " is " + this.action
        );
    }
}

class Bird extends Animal {

    @Override
    public void showAction() {
        System.out.println(
            "The Bird " + this.name + " is " + this.action
        );
    }
}
... 32 lines
˄˄˄
Object    (2/3)

Object

p208 Every class in Java extends class Object. Object is the only class that has no superclass.
 RunCode
class LearningApp {

    public static void main(String[] args) {
        
        Animal a = new Animal(); // extends Object
        
        System.out.println(a.getClass()); 
            // class Animal
                    
        System.out.println(a.hashCode()); // unique object identifier
            // 1169863946 
        
        System.out.println(a.toString());
            // Animal@64c3c749
    }
}

class Animal {}
... 6 lines
˄˄˄

Override

Override annotation acts as a compile-time safeguard. It will throw an error if you're not overriding the superclass method.
 RunCode
class LearningApp {
    public static void main(String[] args) {
        Dog d = new Dog();
    }
}

abstract class Animal {
    public void test() {
    }
}

class Dog extends Animal {

    @Override
    public void testWrongName() { 

        // Throws error:
        // method does not override ...
        // or implement a method from a supertype
    }
}
... 11 lines
˄˄˄

Super

Super() method is used to call parent constructor. The call to super() must be the first statement in each contstructor.
 RunCode
 class LearningApp {
    public static void main(String[] args) {
        Dog d = new Dog();
    }
}

class Dog extends Animal {
    public Dog() {
        super(); // Animal
    }
}

class Animal {
    public Animal() {
        System.out.println("Animal");
    }
}
... 9 lines
˄˄˄
 RunCode
class LearningApp {

    public static void main(String[] args) {
    
        B b1 = new B(); // A
        B b2 = new B(10); // 10
        B b3 = new B("1", "2"); // 12
        
        // B b4 = new B("a"); // Error
    }
}

class B extends A {
    
    public B() {
        super(); // Output: A
    }

    public B(int i) {
        super(i); // Output: 10
    }

    public B(String a, String b) {
        super(a, b); // Output: 12
    }
    
    /*
    public void B(String s) { // void is first statement
        
        super(s);
        
        // Error: Call to super must be the first statement
    }
    */
}

class A {

    public A() {
        System.out.println("A");
    }

    public A(int i) {
        System.out.println(i);
    }

    public A(String a, String b) {
        System.out.println(a+b);
    }

    public A(String s) {
        System.out.println(s);
    }
}
... 33 lines
˄˄˄
Task

 CompleteTask
/**
 * Define Dog class using supertype Animal
 * Use safeguard annotation. 
 */

class LearningApp {
    public static void main(String[] args) {
        
        Dog d = new Dog();
        d.test(); // Dog
    }
}

class Animal {
    public void test() {
        System.out.println(
            this.getClass().getSimpleName()
        );
    }
}

/**
 * SOLUTION
 */

???
solution code
Questions    
10. Polymorphism
09 . Inheritance
Last update:   23-07-2021

        A B C D E F
🔔
1/5