[Java] Superclass / subclass constructors

table of contents

--Introduction --Prerequisite knowledge --About inherited classes --Constructor ――This time --Implicit constructor execution

Introduction

I'm a fledgling engineer. I had to write a continuation of the code written by a person when I was coding at the company the other day. At that time, I got stuck in the class inheritance, constructor </ b> and spent a lot of time ... When I learned from the beginning again, I found something I didn't understand and it was awkward, so I decided to summarize it again.

In addition, this article is ・ "People who are currently touching Java" ・ "People who want to review the content again" It is for. Please read each class name / variable name as appropriate.

Prerequisite knowledge

About inherited classes

--Inherited class = When creating a new class, a class that inherits the same parts as the previously created function --The inheritance source class is called the "parent class", and the inheritance destination class is called the "child class". ――Similarly called "super class" or "subclass"

  • In this article, it is expressed as super / sub. --You can access the methods / fields of the inherited class by creating an instance of the inherited class. --↓↓↓↓ How to write is ↓↓↓↓

Super.java


class Super {
  public void exampleSuper(){
    //Some processing
  }
}

Sub.java


class Sub extends Super(){ //Class name extends Inherit with the inheritance source class name.
  public void exampleSub(){
    //Some processing
  }
}

constructor

--Constructor = A method that is always called automatically when an instance of a class is created. --A method with the same name as the class name --Usually constructor names start with a capital letter for the above reasons --Since the return value is not originally expected, do not write void (→ compilation error) --Mainly used to initialize the fields (member variables) of that class. ――Rather, as usual / implicitly, it's better not to do anything else.
→ Leads to improved readability / maintainability in large-scale development
--↓↓↓↓ How to write is ↓↓↓↓

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 to the bottom. --Subclass constructor first calls superclass constructor --If you do not explicitly call the superclass constructor, the no-argument constructor is automatically called. --The super () </ b> method is implicitly (automatically) executed when the constructor is called.

●super();

A method that calls the superclass constructor. Subclass constructors implicitly (automatically) execute superclass constructors "as a language implementation", so It is not always necessary to describe

--↓↓↓↓ Specific example ↓↓↓↓

Super.java


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

Sub.java


class Sub extends Super {
	Sub() {
		//Implicitly executes superclass constructor
		System.out.println("It's a 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 the constructor of the superclass is defined only with arguments, the implicitly executed "super ();" method alone will cause a compile error.

▽ Solution ① Create a constructor that has no arguments in the superclass

Super.java


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

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

Super.java


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

Sub.java


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

Main.java


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

At the end

It's been a long time again, but thank you for reading. Next time, I think I should deal with exception handling with try-catch. Then.

Recommended Posts