개성있는 개발자 되기

Inheritance 본문

Algorithm/HackerRank

Inheritance

정몽실이 2020. 9. 12. 12:01

Inheritance in Java

Inheritance

We use Inheritance to establish a class hierarchy. A class that inherits from some other class (referred to as a superclass) is called a subclass. While a subclass inherits methods and behaviors from a superclass, it can also declare new fields and methods as well as override superclass methods. Now, let's look at these terms in more detail.

Subclass

We define a subclass using the extends keyword. For example, the syntax ClassB extends ClassA establishes ClassB as a subclass of of ClassA. Java only supports single inheritance, meaning a subclass cannot extend more than one superclass. We can also refer to a subclass as aderived class,extended class, orchild class.

Subclass Constructors

Because a constructor initializes an instance of a class, they are never inherited; however, the subclass must call a superclass constructor as it is an extension of a superclass object. This can be done in either of the two ways shown below.

 

(1) Explicitly Call the Superclass Constructor

In this example, the subclass makes an explicit call to the superclass' parameterized constructor by callingsuper(...);.

// Superclass
class MySuperclass {
    // Superclass' instance variable:
    String myString;

    // Superclass' default (empty) constructor:
    MySuperclass() {} 

    // Superclass' parameterized constructor:
    MySuperclass(String myString) { 
        // Initialize instance variable
        this.myString = myString; 
    }
}

// Subclass
class MySubclass extends MySuperclass {
    // Subclass' constructor:
    MySubclass(String myString) { 
        // Explicit call to superclass' constructor:
        super(myString); 
    }

    public static void main(String[] args) {
        MySuperclass s1 = new MySubclass("Hello, World!");
        System.out.println(s1.myString);

        MySubclass s2 = new MySubclass("Hello, World.");
        System.out.println(s2.myString);
    }
}

 

(2) Implicitly Call the Superclass Constructor

In this example, the subclass makes an implicit call to the superclass' default constructor. This essentially means that the subclass' constructor automatically makes a behind-the-scenes call tosuper();. If a superclass does not have a default constructor, any subclasses extending it must makae an explicit call to one of the superclass' parameterized constructors.

// Superclass
class MySuperclass {
    // Superclass' instance variable:
    String myString;

    // There's no Superclass' default (empty) constructor
}

// Subclass
class MySubclass extends MySuperclass{
    MySubclass(String myString) {
        // A behind-the-scenes implicit call to the 
        // superclass' default constructor happens

        // Subclass can now initialize superclass instance variable:
        this.myString = myString; 
    }

    public static void main(String[] args) {
        MySuperclass s1 = new MySubclass("Hello, World!");
        System.out.println(s1.myString);

        MySubclass s2 = new MySubclass("Hello, World.");
        System.out.println(s2.myString);
    }
}

'Algorithm > HackerRank' 카테고리의 다른 글

Stack and Queue  (0) 2020.09.17
2D Arrays  (0) 2020.09.09
Binary Numbers  (0) 2020.09.08
Comments