A quick review of Java learned in class part4

Introduction

What to handle this time

--Method --Inheritance

Like the previous "constructor", it's hard to understand because it's an object-oriented place. I will describe it with an example. Please read up to part3. If you can understand this part, it will be easier to program in a different language, which is a very convenient way of thinking. Let's do our best.

Method

Here, I will talk about the object-oriented and super important part, the method. A method is a part that describes "what kind of behavior" is given to an object created by oneself. When you create an object called "notebook PC", the constructor describes the object itself such as "who", "color", and "hardware configuration". On the other hand, ** methods can define behavior / state transitions such as "start", "connect to network", and "compress files" **.

java/Car.java


public class Car
{
    int number;
    String owner;
    int speed;

    public Car()
    {
        this(0000, "None", 0);
    }

    public Car(int number, String owner, int speed)
    {
        this.number = number;
        this.owner = owner;
        this.speed = speed;
    }

    void info()
    {
        System.out.println("number:" + this.number);
        System.out.println("owner:"+ this.owner);
        System.out.println("speed:"+ this.speed);
    }
}

java/Main.java


public class Main
{
    public static void main(String[] args)
    {
        Car car1 = new Car(1234, "Takahashi", 40);
        car1.info();
        Car car2 = new Car();
        car2.info();
    }
}

This time I made two cars. One is a simple owner, and the other is a car with nothing set. Car.java's "void info ()" is called by "~ .info ();" in Main.java, and it is executed in order, so all the information is output as information. When an object calls a method, like "car1.info ()", ** describe the method you want to call with "." After the variable name ** and call it. As with the constructor, if the method you call has ** arguments, include them as well **.

...

I want you to wait a moment.

Let's look back on part3.

"Transfer of calculation to a file, but execute by calling the part" int math (int num1, int num2) "written in another file in the Main file"

Yes, you ** have already written a "method with a return value" **. Incorporating object orientation basically doesn't change anything. ** "Methods that can only be executed by objects" ** Is it ** "Methods that the main function itself executes" ** Only changed.

Since the method is the part that describes the behavior of the object, in a sense ** "characteristics", the advantage is that the behavior is the same even if it is called by multiple objects **. This part is useful for the inheritance and interface that we will deal with next.

Summary here

--What is a method? ――What does object-oriented programming do? ――What are the benefits?

Inheritance

In front of...

I really wanted to write inheritance right away, but I'll touch on something that's really useful for doing inheritance with untouched syntax.

It is an array. I haven't done it yet.

As the meaning of the word, it is "a group of arrays arranged in a row from the beginning to the end". However, there are some annoying parts, so I will go carefully.

java/Array.java


public class Array
{
    public static void main(String[] args)
    {
        int[] array = {1, 3, 5, 7, 9};
        /*
↓ Doing the same thing in one line

            int array[] = new int[5]; //Object syntax
            array[0] = 1;
            array[1] = 3;
            array[2] = 5;
            array[3] = 7;
            array[4] = 9;
        */

        for(int i = 0; i < array.length; i++) //array.length represents the length of the array
        {
            System.out.println((i+1) + "Number of items:" + array[i]);
            //The for statement is turning println for 5 lines
            //array[i]By, you can output from the beginning of the element
        }
    }
}

Arrays are hard to understand because the first number of the ** element number (array ** ** [?]) Is from 0 to **. I want you to divide it into such a thing. The syntax is almost the same as the object, but the difference is that [] is added to the name. ** The type is just "int []" **. Of course, it can be double or String, and ** objects can also be inserted **. Note that if you write in one line, "[]" will be attached to the pattern.

The contents of the for statement simply want to output the contents of the array. ** ".length" ** is used to prevent bugs as much as possible. What this means is that ** Java takes care of it without specifying the length of the array **. For example, when you want to manage data using multiple arrays, copying and pasting the same syntax with different elements is a long file and hard to see. In such a case, just knowing this, if you define it as a method, it will be executed with consideration for everything in one shot. Convenient.

Let's inherit based on this.

Inheritance

Before explaining inheritance in Java, let's talk about the idea of inheritance.

** Inheritance means "giving one's own child behavior and characteristics" **. Specifically, the parent "car" can run and stand still, and is heavy and large. And the inherited "supercar" runs very fast, and the weight is a little lighter and bigger. In this way, ** a child that has been improved while retaining some functions and characteristics is called a subclass, and its parent is called a superclass **.

I will create a car this time as well, and create and explain a supercar that inherits it.

java/Car.java


public class Car
{
    double weight;
    int speed;

    Car()
    {
        this(0.0, 0);
    }

    Car(double weight, int speed)
    {
        this.weight = weight;
        this.speed = speed;
    }

    Car(int speed, double weight)
    {
        this(weight, speed);
    }

    void drive()
    {
        System.out.println("Speed" + this.speed + "Ran in");
    }

    void info()
    {
        System.out.println("Weight is" + this.weight + "kg");
        System.out.println("The speed is" + this.speed + "is");
    }
}

java/SuperCar.java


public class SuperCar extends Car
{
    int max_speed;

    SuperCar()
    {
        this(0, 0, 0);
    }

    SuperCar(double weight, int speed, int max_speed)
    {
        super(weight, speed);
        this.max_speed = max_speed;
    }

    void CarChase()
    {
        System.out.println("Speed" + max_speed + "Car chase at");
    }

    void info()
    {
        super.info();
        System.out.println("The maximum speed is" + max_speed + "is");
    }
}

java/Main.java


public class Main
{
    public static void main(String[] args)
    {
        Car normal = new Car(200.0, 40);
        SuperCar superCar = new SuperCar(180.0, 50, 200);

        superCar.drive(); //You can call it even though it is not written!
        System.out.println("\n");
        superCar.info(); 
    }
}

This time, the car element was inherited by the supercar, and the method of the parent class was called by the subclass **. In Java (or rather object-oriented), children are better than parents. It is also possible to create methods and constructors unique to the child class and ** modify the contents of the parent class for the child class **. If you want to call the contents of the parent class as it is, use "super".

Summary here

――What is inheritance? -> Handing down from parent to child ――What is a parent? What is a child? -> The handed down class is "child" ――What is "super"? -> Required when a child calls a parent

I will do multiple inheritance, "static", "public", "private", "protected", etc. next time. While using arrays ...

Recommended Posts

A quick review of Java learned in class part4
A quick review of Java learned in class part3
A quick review of Java learned in class part2
A quick review of Java learned in class
Creating a matrix class in Java Part 1
A quick explanation of the five types of static in Java
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 10)
What is a class in Java language (3 /?)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 9)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 6)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 4)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 5)
Handle business logic for a set of Entity in Java class
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 2)
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 1)
Creating a matrix class in Java Part 2-About matrices (linear algebra)-
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 11)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 12)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 8)
Measure the size of a folder in Java
What I learned in Java (Part 2) What are variables?
NIO.2 review of java
Review of java Shilber
Java inner class review
NIO review of java
Cause of is not visible when calling a method of another class in java
Write a class that can be ordered in Java
Browse class objects in Kotlin (instead of Java class name.class)
Write a class in Kotlin and call it in Java
What I learned in Java (Part 3) Instruction execution statement
What I learned when building a server in Java
Use of Abstract Class and Interface properly in Java
Summarize the additional elements of the Optional class in Java 9
[Java] Comparator of Collection class
Find a subset in Java
Summary of Java Math class
Implementation of gzip in java
Implementation of tri-tree in Java
What I learned in Java (Part 4) Conditional branching and repetition
Activate Excel file A1 cell of each sheet in Java
[MQTT / Java] Implemented a class that does MQTT Pub / Sub in Java
I tried to make a client of RESAS-API in Java
Think of a Java update strategy
Let's create a TODO application in Java 4 Implementation of posting function
Various methods of Java String class
What I have learned in Java (Part 1) Java development flow and overview
3 Implement a simple interpreter in Java
I created a PDF in Java.
I can't create a Java class with a specific name in IntelliJ
Let's create a TODO application in Java 6 Implementation of search function
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
Let's create a TODO application in Java 8 Implementation of editing function
Sort a List of Java objects
Basic usage of java Optional Part 1
StringBuffer and StringBuilder Class in Java
Mechanism and characteristics of Collection implementation class often used in Java
A simple sample callback in Java
List of members added in Java 9