[Java] [Java] Superclass/Subclass constructors

3 minute read

table of contents

  • Introduction
  • Prerequisite knowledge -About inherited classes -Constructor
  • This time -Implicit constructor execution
  • in conclusion

Introduction

I’m a fledgling engineer. When I was coding at the company the other day, I had to write the continuation of the code written by other people, At that time, I was caught in a pot with class inheritance, constructor and spent a considerable time …. When I re-learned from the beginning again, I was terrified that I did not understand, so I decided to put it together again.

In addition, this article · “People who are currently touching Java” ・”People who want to review the contents again” It is intended for Please replace each class name/variable name appropriately.

Prerequisite knowledge

About inherited classes
  • Inheritance class = When creating a new class, a class that inherits the same parts as the functions created previously
  • The inheritance source class is called a “parent class”, and the inheritance destination class is called a “child class”.
  • Also called “super class” and “sub class” with the same meaning
    • Expressed as super/sub in this article.
  • You can access the methods/fields of the inherited class by creating an instance of the inherited class.
  • ↓ ↓ ↓ ↓ How to describe ↓ ↓ ↓ ↓

Super.java


class Super {
  public void exampleSuper(){
    // do something
  }
}

Sub.java


class Sub extends Super(){ // Class name extends Inherited with inherited class name.
  public void exampleSub(){
    // do something
  }
}

constructor

  • Constructor = method that is always called automatically when an instance of the class is created
  • A method with the same name as the class name
  • Usually, the constructor name starts with a capital letter for the above reason
  • Since the return value is not originally assumed, do not write void (→ compilation error)
  • Mainly used to initialize fields (member variables) of the class.
  • Or rather / as it seems implicitly, it’s better not to do anything else.
    →Improve readability/maintainability in large-scale development
  • ↓ ↓ ↓ ↓ How to describe ↓ ↓ ↓ ↓

Smple.java


class Sample{
  public Sample(){
    // Here is the constructor
  }
  public void exampleMethod(){
    // this is just a class method
  }
}

This time

Implicit constructor execution

  • When instantiating a class, it is instantiated in order from the top of the inheritance hierarchy downwards
  • Subclass constructor first calls superclass constructor
  • If you don’t explicitly call the constructor of the superclass, the constructor without arguments is called automatically.
  • super() method is executed implicitly (automatically) when the constructor is called

● super();

A method to call the superclass constructor. The subclass constructor implicitly (automatically) executes the superclass constructor “as the language implementation”, so It is not always necessary to describe

  • ↓ ↓ ↓ ↓ Concrete example ↓ ↓ ↓ ↓

Super.java


class Super {
Super() {
System.out.println("Super class!");
}
}

Sub.java


class Sub extends Super {
Sub() {
// The superclass constructor is executed implicitly
System.out.println("Subclass!");
}
}

Main.java


class Main {
public static void main(String[] args) {
Sub obj = new Sub();
}

//Execution result
It's a super class!
It's a subclass!

“Implicit super constructor is undefined”

  • If only the constructor of the superclass is defined with arguments, the “super();” method that is executed implicitly causes a compile error.

▽ Solution ① Create a constructor with no arguments in the superclass

Super.java


class Super {
    public name;
    public Super(){
        // overload the constructor
    }
    public Super(String name){
        this.name = name;
    }
}

② In the constructor of the subclass, call the superclass constructor explicitly with the super(); method that matches the arguments of the superclass constructor.

Super.java


class Sub {
    public name;
    public Super(String name){
        // Set the value of the name variable passed in the order of main -> sub -> super to the field
        this.name = name;
    }
}

Sub.java


class Sub extends Super {
    Sub(String name) {
    // Corresponds to the Strin type variable name of the main class -> Pass it to the constructor of the super class
    super(name);
    }
}

Main.java


class Main {
public static void main(String[] args) {
Sub obj = new Sub(String name);
}
}

At the end

Thank you for reading this time as well. Next time, I’d like to talk about exception handling with try-catch. Then.