Java starting from beginner, override

Introduction

This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ~~ ** The mistaken ** is the center. This is for the purpose of posting the mistaken part during coding and posting it for self-reflection ~~. In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.

environment

Language: Java11, JDK13.0.2 Operating environment: Windows 10

override

Last time I dealt with the relationship between superclasses and subclasses.

The subclass inherits the superclass method, but sometimes you want to define another process while using the same method name. For example, when preparing a method for checking members with show 〇〇 (). Even if you check the same, you will be able to use watch 〇 〇 (), see 〇 〇 (), check 〇 〇 () properly. However, different arguments and types make it very difficult to remember and use, and the unnecessarily long code reduces readability. In the subclass, you can define a new method that is exactly the same as the one implemented in the ** superclass, and when calling from an object, the subclass takes precedence. ** **

As a condition,

  1. Same signature (method name, argument list type, number, order)
  2. Same or looser access modifier
  3. The return type is the same in principle (excluding covariant return values)

The fact that subclass methods act on behalf of superclass members is called ** overriding **.

Handle all together in superclass

As I write many times, subclasses inherit superclass members. And by overriding, you can write a process dedicated to the subclass and make it function preferentially. Therefore, when using a subclass that is a child of the same superclass, the processing can be described collectively by the method of the superclass.

arrayCatsShow.java


class WildCat
{
  protected int number;
  protected double weight;

  public WildCat()
  {
    number = 1;
    weight = 1.0;
  }
  public void getCat(int n,double w)
  {
    number = n;
    weight = w;
    System.out.println(number +"The weight of the second cat" + weight + "It is kg.");
  }

  public void show()
  {
    System.out.println("This cat" + number + "This is the second cat.");
    System.out.println("The weight of this cat" + weight + "It is kg.");
  }
}

Based on this WildCat class, prepare HouseCat and MyCat classes.

arrayCatsShow.java


class HouseCat extends WildCat
{

  public void show()
  {
    System.out.println("This domestic cat" + number + "It is the second." );
  } 
}

class MyCat extends WildCat
{
  public void show()
  {
    System.out.println("Of these cats" + number + "The current weight is" + weight + "was." );
  }
}

Then prepare an individual cat.

arrayCatsShow.java


class arrayCatsShow
{
  public static void main(String[] args)
  {
    WildCat[] cats = new WildCat[4];
 
    cats[0] = new WildCat();
    cats[0].getCat(1, 5.2);

    cats[1] = new HouseCat();
    cats[1].getCat(2, 4.2);

    cats[2] = new MyCat();
    cats[2].getCat(3, 5.6);

    cats[3] = new MyCat();
    cats[3].getCat(4, 8.4);

    for(int i=0; i< cats.length; i++){
      cats[i].show();
    }
  }
}

Since we prepared an array of superclasses and then new individually, sentences based on eachshow ()definition appear.

When you just want to add

When you want to process superclass + subclass.

HouseCatShow.java


class HouseCat extends WildCat
{
  public void show()
  {
    super.show();
    System.out.println("This domestic cat" + number + "It is the second." );
  }
}

You don't have to write everything again because you can bring the superclass processing as it is with super.method name. This is also true for fields, so you can also bring in superclass values with super.field.

By the way, if you don't want to override the superclass method, you can add ** final likepublic final void show ()and you can't override it **. This can also be done with the class name and field, so if it's a final class, it's ** a subclass that can't be extended , and if it's a final field, the value of the ** field doesn't change except for initialization at declaration. constant) `.

in conclusion

I found the word ** covariant return value ** in the Silver problem book, but I'm going to examine the class library soon, so I'd like to clarify the relationship between the classes before dealing with it.

reference

I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.

Easy Java 7th Edition Java SE11 Silver Problem Collection (commonly known as Kuromoto)

Recommended Posts

Java starting from beginner, override
Java, instance starting from beginner
Java starting from beginner, inheritance
Java overload constructor starting from beginner
Java starting from beginner, variables and types
Java starting from beginner, nested / break / continue
Java, if statement / switch statement starting from beginner
Java, for statement / while statement starting from beginner
Java starting from beginner, class declaration / object generation
Java life starting from scratch
java (override)
java beginner 4
java beginner 3
java beginner
Java, abstract classes starting from beginners
Java, interface to start from beginner
IntelliJ starting from 1
Java starting from beginners, logical operators / conditional operators
Getting Started with Java Starting from 0 Part 1
Java Beginner Exercises
Java Exercise "Beginner"
For Java engineers starting Kotlin from now on
[Java] Overload / Override / Inheritance
Call Java from JRuby
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Eval Java source from Java
Picture-in-picture starting from iOS14
Access API.AI from Java
From Java to Ruby !!
[Java] Overload and override
[Java] Platforms to choose from for Java development starting now (2020)
Migration from Cobol to JAVA
Progate Java (Beginner) Review & Summary
Creating ElasticSearch index from Java
New features from Java7 to Java8
Connect from Java to PostgreSQL
Using Docker from Java Gradle
From Ineffective Java to Effective Java
JavaScript as seen from Java
[Beginner] Java basic "array" description
Execute non-Java instructions from Java
Solve AtCoder Beginner Contest 151 in java
Call Kotlin's sealed class from Java
Differences between "beginner" Java and Kotlin
Solve AtCoder Beginner Contest 150 in java
Code Java from Emacs with Eclim
Deep Learning Java from scratch 6.4 Regularization
Solve AtCoder Beginner Contest 153 in java
[Java beginner] About abstraction and interface
Get country from IP address (Java)
Run node.js from android java (processing)
Run a batch file from Java
[Java] Remove whitespace from character strings
Akka hands-on preparation procedure from Java
[Java beginner] == operator and equals method
Access Teradata from a Java application
Solve AtCoder Beginner Contest 175 in java
Use Chrome Headless from Selenium / Java
Java to be involved from today
From Java to VB.NET-Writing Contrast Memo-