Java starting from beginner, inheritance

Postscript 2020/3/16 I corrected it because I was pointed out in the comments that there was a mistake in the way of naming the class.

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 part that was actually mistaken 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

Class extension

All the classes I've written so far have been written independently, but I had to set members (fields and methods) each time. If it's about the Cat class, you can make it if you do your best, but it's not realistic to write every class with a huge number of members (fields and methods) every time. Java has a ** class extend ** function to create your own class while inheriting the members of the existing class.

extendedHousecat


class WildCat
{
  protected double weight;
  protected double height;
  protected String name;

  public static int sumCats = 0;

  public wildCat()
  {
    weight = 0.0;
    height = 0.0;
    sumCats++;
    System.out.println("Nyago, I found another cat.");
  }
 
  public WildCat(double we,double he)
  {
    weight = we;
    height = he;
    sumCats++;
    System.out.println("Nyago, I found another cat.");
    System.out.println("The cat I found is the weight" + weight + "kg, height" + height + "It looks like cm.");
  }

  public void measureWeightHeight(double w,double h)
  {
    weight = w;
    height = h;

    System.out.println("I measured this cat.");
    System.out.println("Weight" + weight + "kg, height" + height + "There is cm.");
  }

  public static void countCats()
  {
    System.out.println("In all" + sumCats + "I found a cat.");
  }
}

//Wildcat(Wildcat)Based on the class of(Housecat)Let's make a class.

class HouseCat extends WildCat
{
  private String collorColor;
  private String nickname;

  public void callNickname(String s)
  {
    nickname = s;
    System.out.printlm("New to this cat" + nickname + "I gave it a nickname.");
  }
}

The wildcat (WildCat) class was extended to create a domestic cat (HouseCat, Cat ... there are various names) class. At this time, Wildcat and HouseCat have a parent-child relationship, which is super class and sub class, respectively.

Is it a supermarket or a sub?

The HouseCat class, which is an extension of the WildCat class, inherits the members (fields and methods) of the parent WildCat class.

HouseCat cat1 = new HouseCat(); cat1.measureWeightHeight(5.0,50.5);

By doing so, you can create a HouseCat object with a weight of 5.0 kg and a height of 50.5 cm. Of course, it also has a collorColor and nickname fields, You can also give it a nickname with the public void callNickname () method.

I dare to use the parent constructor

The HouseCat class does not have a constructor, but if you do not provide a constructor in the sub class, when the ** no-argument constructor ** of the super class constructor creates a HouseCat object Is automatically called to. This time it's public WildCat (). However, for example, if you call it as super (8.0,60.8) in the constructor, it will be called as a domestic cat of 8.0kg, 60.8cm.

Introverted than private, sociable than private

Did you notice that the fields in the WildCat class are different than usual? (Actually, I fixed it after writing so far). protected is a modifier that means" accessible from the same class or subclasses that are children. " If you can expect to become a superclass that you want to create and develop more and more subclasses, it will be easier to handle later if you set it to protected. As for access restrictions, we are refraining from packaging, but we will omit it here.

At the end

What the inherited subclass must have, and where do superclass members allow access? I'm still suffering from these problems, so the current situation is that my understanding here is still weak. The Java 11 Reference that I dealt with before also showed ʻextends` in a mess with just a little peek. There is. When you understand this family tree, you'll probably see the real Java.

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)

Java® Platform, Standard Edition & Java Development Kit Version 11 API Specification

Recommended Posts

Java starting from beginner, inheritance
Java starting from beginner, override
Java, instance starting from beginner
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 beginner 4
[Java] Inheritance
java beginner 3
Java inheritance
java beginner
Java inheritance
java (inheritance)
Java, abstract classes starting from beginners
Java, interface to start from beginner
[Java] Class inheritance
IntelliJ starting from 1
Java starting from beginners, logical operators / conditional operators
Java Beginner Exercises
About java inheritance
Java Exercise "Beginner"
For Java engineers starting Kotlin from now on
Call Java from JRuby
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Eval Java source from Java
Summarize Java inheritance (Java Silver 8)
Picture-in-picture starting from iOS14
Access API.AI from Java
About inheritance (Java Silver)
From Java to Ruby !!
[Java] Implicit inheritance memo
Java learning memo (inheritance)
[Java] Platforms to choose from for Java development starting now (2020)
Advanced inheritance abstract, interface -java
Migration from Cobol to JAVA
JAVA learning history interface inheritance
Progate Java (Beginner) Review & Summary
Creating ElasticSearch index from Java
New features from Java7 to Java8
Connect from Java to PostgreSQL
[Java] What is class inheritance?
java (inheritance of is-a principle)
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
Muscle Java Object Oriented Day 2 ~ Inheritance ~
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