Java 3 major elements (object-oriented) memorandum

Introduction:

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.

Java 3 major elements
Inheritance
Polymorphism
Encapsulation

1. Inheritance

What is inheritance?

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();
		//
	}

}


2. Polymorphism

What is polymorphism?

One method has the same name and works differently. It is mainly realized by the following two mechanisms.

·Overload ·override

What is overload?

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.

What is an override

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.

3. Encapsulation

What is encapsulation?

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〇〇

At the end

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

Java 3 major elements (object-oriented) memorandum
Java memorandum
JAVA memorandum
[Java] Object-oriented
Java memorandum (list)
[Java] Object-oriented summary_Part 1
[Java] Object-oriented syntax-Constructor
Object-oriented (Java) basics
[Java] Object-oriented summary_Part 2
Java study memorandum
[Java] Optional memorandum
[Java] Object-oriented syntax-Package
Memorandum of new graduate SES [Java object-oriented edition]
webApi memorandum in java
Java8 Silver exam memorandum
Java8 Gold exam memorandum
Object-oriented (java) with Strike Gundam
Object-oriented summary by beginners (Java)
[Java] Delete the elements of List
[Java] Object-oriented syntax --class method / argument
[Java] Object-oriented syntax-class / field / method / scope
Summary of object-oriented programming using Java