[JAVA] Inheritance and interface.

For myself. I'll use java below.

I don't think it's really good, but I haven't used these syntaxes yet, so there's probably a misunderstanding. Write the code in your spare time, move it, and huh ...? I'll do it, so wait.

Inheritance

Inheritance. That means extends. The inherited class is called super class or parent class. The inherited class is called a child class.

Since the child class has all of the super class, you can use the variables and methods of the super class. Those with the private access modifier of the super class cannot be used. Any protected modifier can be used.

The following is a class of mysterious creatures that can only run and sleep. The child class just calls the function of the parent class for the time being.

A creature that consumes 10 stamina when running and recovers 10 stamina when sleeping.

sperClass.java


class superClass(){
    stamina = 100;

    protected void run(){
        stamina -= 10;
    }

    protected void sleep(){
        stamina += 10;
    }
}

childClass.java


class childClass extends superClass{
    public voic main(){
        System.out.println(stamina);
            //100 (The value of the stamina variable of the parent class is displayed)

        run();
        sleep();
            //You can use the methods of the parent class
    }
}

Override I want to rewrite a method such as super class in a child class. Override is what you do at such times. The method can be overwritten.

Let's turn the mysterious creature that runs and sleeps into a cheat-class bug creature A creature that consumes 5 stamina when running and recovers 50 stamina when sleeping.

sperClass.java


class superClass(){
    int stamina = 100;

    protected void run(){
        stamina -= 10;
    }

    protected void sleep(){
        stamina += 10;
    }
}

childOverride.java


class childOverride extends superClass{
    @Override
    private void run(){
        stamina -= 5;
    }

    @Override
    private void sleep(){
        stamina += 50;
    }

    public void main(){
        //Something appropriate processing
    }
}

Yes. There are some creatures.

By redefining the method with the same name in the child class, it replaces the content defined in the child class.

@Override is to make it clear to the compiler that the following method is trying to override, and it seems that there is no problem in operation even if it is not written. However, if you add @Override, an error will be issued if the method with the same name does not exist in the super class when compiling, so you can find it before running the bug.

Only methods can be overridden, not variables.

super keyword

I want to run the constructor of the super class. I want to use the function of super class before override. I want to reassign a variable of super class.

The super keyword is used in such cases.

When an instance of a child class is created, an instance of the super class is also created without permission, but it is useful when you want to pass an argument to the constructor of that super class.

super(param, args);

If you call it like this, you can pass arguments to the constructor. You can call the constructor of the super class at any time, but it is not recommended except from the constructor of the child class, so you should not do it.

interface

It seems good to think that the interface is like a blueprint. This is to clearly state "Please prepare such a function".

public interface actions{
    void run();
    void sleep();
    void eat();
    void jump();

So, I will write a class to implements according to this.

class Hito implements antions{
    @Override
    public void run(){
        //Sprint with two legs
    }

    @Override
    public void sleep(){
        //Sleep with a futon
    }

    @Override
    public void eat(){
        //Eat with a spoon and fork
    }

    ...

}

This is a person. You can run at full speed with two legs.

class Dog implements antions{
    @Override
    public void run(){
        //Sprint with four legs
    }

    @Override
    public void sleep(){
        //Roll up and sleep in your favorite place
    }

    @Override
    public void eat(){
        //Go directly to the plate with your mouth
    }

    ...

}

This is a dog. You should run on four legs, roll up your sleep, and eat using your mouth only. If it is a general dog.

With this kind of feeling, the interface is to write the design and implementation separately. ʻImplements` means "implementation". Is it like a template when creating a class? Feel.

Abstract class (abstract)

A class that always requires inheritance, like the interface mentioned above, is called an abstract class. If you attach the ʻabstruct modifier to a class, you will not be able to instantiate it directly, and if you attach it to a method, you will not be able to call that method as it is. All classes that contain methods with the ʻabstruct qualifier must have the ʻabstruct` qualifier.

You shouldn't create an instance of a class with unfinished methods.

public abstruct class action{
    int O2;
    void Kokyu(){
        O2 += 10;
    }

    abstruct void run();
    abstruct void sleep();
    abstruct void eat();
}
class Hito extends action{
    @Override
    void run(){
        //Run
    }

    @Override
    void sleep(){
        //Sleep with a futon
    }

    ...

    public void main(){
        Kokyu();
            //Breathe (super class method)

        run()
            //Run

        sleep();
            //sleep
    }
}

eh...?

Yes Yes. I know... You'll want to say real interface yanke </ b>. I understand I understand.

In the case of interface, one class can be fully implemented for multiple interfaces. However, inheritance cannot be inherited by one class.

Variables in interface have static final modifiers and become constants, so they cannot be reassigned. Also, all the methods written in interface are like ʻabstruct`, so no processing is applied.

Since the base of an abstract class is a class, you can write methods that are not ʻabstruct`, and you can also have variables. It's a partially unfinished class, so the atmosphere is that you should use it after completing it properly.

Blueprints </ b> and Unfinished Classes </ b>. There is such a difference.

Digression: is-a and has-a

is-a is-a is A is a B. The idea that A is B.

A is more like B. B seems to be in the base. Another C is also B-like. B seems to be in the base.

Then both A and C can be made based on B. That (I think)

has-a has-a is A has a B. The idea that A has B.

A has something like B. Another C also has something like B. Then, let's make it possible to share this B-like thing. That (I think)

reference

[Java] What is an override? Implement the interface! How to use implements in Java [For beginners] Which should I use, interface or abstract class?

Recommended Posts