[Easy-to-understand explanation! ] How to use Java inheritance [Override explanation]

1. Prior knowledge

-[Latest] How to build Java environment on Ubuntu

-[Even beginners can do it! ] How to create a Java environment on Windows 10 (JDK14.0.1)

-[Easy-to-understand explanation! ] How to use Java instance

-[Even beginners can do it! ] How to install Eclipse on Windows 10 (Java environment construction)

As prior knowledge, the contents of the above link are required.

2. What is inheritance?

--Inheritance is to define a class with extensions and changes based on the class already defined. --The class that is the source of inheritance is called " superclass ". --The class newly defined by inheritance is called " subclass ". --Define subclass with" subclass name extends superclass name". --In subclass, describe only the difference from the superclass. --Java allows only superclass to be defined for one class due to language specifications. --You can create a subclass by furtherinheriting thesubclass. --All classes are [java.lang.Object class](https://docs.oracle.com/javase/jp/7/api/java/lang) defined in the Java Standard Class Library (Java API). /Object.html) is inherited. --Classes with the final modifier cannot inherit. --Subclasses cannot directly access the private variableof the superclass. --In subclass instantiation, thesuperclass constructor is processed and then the subclass constructoris processed. --You can arbitrarily call thesuperclass constructor by writing super () at the beginning of the subclass constructor. --You can redefine a method defined in a superclass in a subclass by using override`.

3. What is an override?

--Override means to redefine a method defined in a superclass in a subclass. --Override is used when you want to perform different processing with the same method name / argument as the superclass method. --A basic description example of override is as follows.

Super class


class superclass name{

    //Superclass method
Method name(){
        //processing
    }
}

Subclass


class subclass name extends superclass name{

    //override
    @Override
Method name(){
        //processing
    }
}

4. Basic writing of inheritance

Test class


package package name;
public class main class name{
    public static void main(String[] args) {
        //Instance generation
Subclass name variable name=new subclass name();
        //Put a value in setter
Variable name.set instance variable name(Actual argument);
        //Get the value entered by getter
        System.out.println(Variable name.getインスタンスVariable name());
    }
}

Super class



package package name;
class superclass name{
    //Definition of instance variables
private type name variable name;

    //Constructor (executed when instantiating)
Super class name(Type name argument){
Initialization process, etc.
    }
    // setter
void set instance variable name(Type name Argument name){
        this.Variable name=Argument name;
    }
    // getter
Type name get instance variable name(){
return variable name;
    }
}

Subclass


package package name;
class subclass name extends superclass name{
    //Definition of instance variables
private type name variable name;

    //Constructor (executed when instantiating)
Subclass name(Type name argument){
        super(argument);
Initialization process, etc.
    }
    // setter
void set instance variable name(Type name Argument name){
        this.Variable name=Argument name;
    }
    // getter
Type name get instance variable name(){
return variable name;
    }
}

--Basic encapsulation is described as above.

5. Preparation

01.png

  1. Start Eclipse and select [File (F)]-> [New (N)]-> [Java Project]. 02.png
  2. Enter Test1 for the project name and click the Done button. 03.png
  3. Select [File (F)] → [New (N)] → [Class]. 05.png
  4. Enter Test1 for the package and name and click the Done button. 06.png
  5. Confirm that Test1.java has been created. 01.png Enter Test1 in the package, enter TestSuper in the name, and click the Finish button in the same way as in 6.3. 02.png
  6. Confirm that TestSuper.java has been created. 03.png Enter Test1 in the package and TestSub in the name in the same procedure as in 8.3, and click the Finish button. 04.png
  7. Success if Test1.java, TestSuper.java, and TestSub.java are created.

6. Description example

Test1.java


package Test1;
public class Test1 {
	public static void main(String[] args) {
        //Instance generation
		TestSuper ts1 = new TestSuper("A");
		TestSub ts2 = new TestSub("B","Gyoza");

		//View instance
		ts1.showName();
		System.out.println();
		ts2.showName();
    }
}

TestSuper.java


package Test1;
public class TestSuper {
	//Definition of instance variables
    private String name;

    //Constructor (executed when instantiating)
    public TestSuper(String name) {
		this.name = name;
	}
    // setter
	public void setName(String name) {
		this.name = name;
	}
	// getter
	public String getName() {
		return name;
	}
	public void showName() {
		System.out.println("name:"+name);
	}
}

TestSub.java


package Test1;
public class TestSub extends TestSuper {
	//Definition of instance variables
    private String food;

 //Constructor (executed when instantiating)
	public TestSub(String name,String food) {
		super(name);
		this.food = food;
	}
	// setter
	public void setFood(String food) {
		this.food = food;
	}
    // getter
	public String getFood() {
		return food;
	}
	@Override
	public void showName() {
		super.showName();
		showFood();
	}
	public void showFood() {
		System.out.println("Favorite food:"+food);
	}
}

--Copy the above sentence, specify S-JIS as the character code, save the file name as Test1.java, TestSuper.java, TestSuper.java, and execute it. .. ↓ ↓ 01.png

7. Related

-[Useful to remember !!!] Easy creation of constructor and getter / setter in Eclipse -[Even beginners can do it! ] How to write Javadoc -[Easy-to-understand explanation! ] How to use Java encapsulation -[Easy-to-understand explanation! ] How to use Java overload

Recommended Posts

[Easy-to-understand explanation! ] How to use Java inheritance [Override explanation]
[Easy-to-understand explanation! ] How to use Java polymorphism
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Easy-to-understand explanation! ] How to use Java overload
[Easy-to-understand explanation! ] How to use Java encapsulation
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
How to use Java HttpClient (Post)
[Java] How to use join method
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
[Java] How to use the hasNext function
How to use submit method (Java Silver)
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Java classes, definitions, import
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
[Processing × Java] How to use the function
[Java] How to use the Calendar class
[Java] Learn how to use Optional correctly
try-catch-finally exception handling How to use java
[Java] Overload / Override / Inheritance
[Java] How to use FileReader class and BufferedReader class
[Java] How to use Thread.sleep to pause the program
How to use Map
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Java framework with AWS Lambda! ??
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use TreeSet
How to use Java API with lambda expression
[How to use label]
How to use hashes
How to use the replace () method (Java Silver)
How to use JUnit 5
How to use Alibaba Cloud LOG Java Producer
How to use Dozer.mapper