This is the 10th day of the external training, so I will summarize it as a reflection for myself.
A major feature of Java is object orientation. Speaking of object-oriented features ... I will write the following because it will be the following three.
In Java, it is possible to create a new class based on an existing class. The above is called extends.
Inheritance is when a newly extended class inherits members of an existing class.
Example) In the training, it was a car, so let's go with an animal.
inheritance.java
package lesson11_second;
//Super class animals
class Animal {
protected int age;
protected String name;
protected final static String TRIBE = "animal";
protected String settingMessage;
public Animal() {
this(TRIBE);
age = 0;
name = "noname";
}
//Overriding tribe for sorting
public Animal(String TRIBE) {
birth(TRIBE);
}
public Animal(int age, String name) {
this();
this.age = age;
this.name = name;
System.out.println("Age" + age + "age" + "\t"
+ "Name" + name + "I made it.");
}
public void birth(String TRIBE) {
System.out.println(TRIBE + "Was born.");
}
public void setAnimal(int age, String name) {
this.age = age;
this.name = name;
}
public String getAnimal() {
String settingMessage = "Age" + this.age + "age" + "\t"
+ "Name" + this.name + "I made it.";
return settingMessage;
}
public void show() {
System.out.println("Age is" + age + "I'm old.");
System.out.println("Name is" + name + "is.");
}
}
//Subclass human
class People extends Animal {
private String favoriteFood;
protected final static String TRIBE = "Human";
//Class distribution by class variable
public People() {
super(People.TRIBE);
favoriteFood = "meat";
}
public People(int age, String name, String favoriteFood) {
super(age, name);
this.favoriteFood = favoriteFood;
System.out.println("Age" + age + "age" + "\t"
+ "Name" + name + "Favorite food" + favoriteFood + "I made it.");
}
public void setFavoriteFood(String favoriteFood) {
this.favoriteFood = favoriteFood;
System.out.println("Favorite food" + favoriteFood + "I made it.");
}
public void show() {
System.out.println("");
super.show();
System.out.println("What is your favorite food" + favoriteFood + "is.");
}
}
public class inheritance {
public static void main(String[] args) {
Animal animal = new Animal();
Animal animal2 = new Animal(20, "Sato");
People people = new People();
People people2 = new People(40, "Jiro Tanaka", "hamburger");
people.setAnimal(20, "Yamada");
people.setFavoriteFood("curry");
people.show();
//
}
}
One method has the same name and works differently. It is mainly realized by the following two mechanisms.
·Overload ·override
If the method name is the same but the method argument types and numbers are different Different processes can be described in the same class. At the time of calling, the function to be called can be specified by the argument given to the method.
The method name is the same, and the type and number of method arguments are also the same. It is used in a class (subclass) that uses inheritance and overwrites the function of the original method.
You can restrict access to classes, fields, and methods. Convenient when developing with multiple people. By restricting access to areas that you do not want to be repaired Call attention. (Apart from final).
For access restrictions, describe the access modifier before the target to be restricted. There are four types of access modifiers below. ・ Public ・ Protected ・ No description ・ Private
The method name that updates the value of a field with access restrictions is set〇〇 The method name to get the value of the field with access restrictions is get〇〇
I wrote it, but I forgot to post it. I posted this two days after the schedule ...
During the training, I set the goal of posting this article once a week, so I would like to do my best to achieve it.
I wrote it in my memorandum, but I would appreciate it if you could point out if the contents are incorrect. Please forgive me because I am still a newcomer.
Recommended Posts