Object-oriented summary by beginners (Java)

Introduction

I wrote about the object-oriented programming I learned in the training as a memo for myself. I chew and summarize the contents that I have clogged or investigated.

Contents

·class ·instance ・ Member variables ・ Method ·constructor ・ Class variables (static variables) ・ Inheritance ·override

Rough explanation

・ Class: Template, template (ex. Human) -Instance: An entity created based on a class (ex. A, B, ...)

-Member variables: Instance attributes (ex. Physical strength, intelligence, age) -Method: Action that the instance can do (ex. Walk, say hello) -Constructor: Initial setting of member variables

-Class variable (static variable): Class attribute (If you rewrite Mr. A's data, Mr. A's data and Mr. B's data will be changed in the same way)

-Inheritance: A mechanism that allows you to use the functions of the original class (superclass) when deriving a class -Override: When inheriting a class, redefining (overriding) the method (processing) of the superclass in the subclass.

Example

At the bottom, I wrote the code that shows the relationship between classes, instances, inheritance, etc., so please read the explanation while comparing it.

Classes and instances

After making a human model, create a Japanese model with human characteristics, Based on that, we are creating a person named Taro.

At this time, humans are superclasses, Japanese are subclasses, and Taro is an instance.

Member variables

Member variables are the status of an instance. This time, humans will have the status of physical strength, intelligence, and language.

constructor

This is the initial setting of member variables. In this case, human physical strength and intelligence are set by arguments, and the language is set to null by default.

Also, since Japanese is my mother tongue, I set the language to Japanese in the Japanese class (Japanese template).

Method

We have set two actions that all humans can do: walking and greeting.

In addition, as a behavior peculiar to Japanese, the behavior of bowing was set in the Japanese class.

Furthermore, greetings are actions that humans commonly perform, but the form of their output is If the Japanese "Hello", different from the like if the Chinese "Nihao". Therefore, you overload the greet method in Japanese class, we try to greet with "Hello".

================

Human.java


//Class (human template)
public class Human{

	//Member variables (instance attributes)
	int pysicalPower; //Physical fitness
	int intellectualPower; //Intelligence
	String langage; //language

	//Class variable (static variable)
	static int magicPower = 0;  //Since it is static, all humans have 0 magical power. If someone's magic power is set to 10, everyone else will also have magic power of 10.

	//Constructor (initial setting of member variables)
	public Human(int pysical, int intelligence) {
		this.pysicalPower = pysical;
		this.intellectualPower = intelligence;
		this.langage = null;
	}

	//Method (behavior)
	public void walk() {  //walk
		System.out.println("Tech tech");
	}
	public void greet() {  //Greet
		System.out.println("Hello");
	}

	//Setting confirmation method
	public int getPysicalPower() {
		return pysicalPower;
	}
	public int getIntellectualPower() {
		return intellectualPower;
	}
	public String getLangage() {
		return langage;
	}
}

Japanese.java


//Class (Japanese template)
public class Japanese extends Human { //A subclass that inherits from the Human class

	//constructor
	Japanese(int pysical, int intelligence) {
		super(pysical, intelligence); //Call parent constructor
		super.langage = "Japanese";
	}

	//Method
	public void ojigi() {  //to bow
		System.out.println("Pecoli");
	}

	//override
	@Override
	public void greet() {
		System.out.println("Hello");
	}
}

Main.java


public class Main {

	public static void main(String[] args) {

		//Generate an instance (individual based on a template)
		Japanese Taro = new Japanese(50, 30);  /*Japanese Taro(Physical strength 50, intelligence 30)Generate a*/

		//Check Taro's status
		System.out.println("Taro's physical strength" + Taro.getPysicalPower());
		System.out.println("Taro's intelligence" + Taro.getIntellectualPower());
		System.out.println("Taro's language is" + Taro.getLangage());
		System.out.println(); //Blank line


		//Call a method (behavior)
		Taro.walk(); //Human class methods

		Taro.ojigi(); //Japanese class method

		Taro.greet(); //Method overridden in Japanese class

	}

}

Execution result


Taro's physical strength is 50
Taro's intelligence is 30
Taro's language is Japanese

Tech tech
Pecoli
Hello

in conclusion

When I thought about and wrote the code that actually uses object orientation, I got a little deeper understanding. There are some parts that I couldn't explain well because I didn't understand well, and some contents that I didn't include so as not to be complicated, so I think it might be a good idea to write the second one as the learning progresses. (If you feel like it ...)

reference

Sites that I referred to when learning

-Thorough explanation for beginners! What is object orientation? (Especially, once I copied the part of "object-oriented programming learned from heroes", I understood it considerably) -What is a member variable? | An IT terminology dictionary that makes you feel like you understand even if you don't understand it

Recommended Posts

Object-oriented summary by beginners (Java)
Object-oriented summary
[Java] Object-oriented
[For beginners] Summary of java constructor
Object-oriented course for beginners sent by beginners
Java "pass by reference" problem summary
Summary of object-oriented programming using Java
Java knowledge summary
Java Generics Summary
Java related summary
Object-oriented FizzBuzz (Java)
[Java] Object-oriented syntax-Constructor
Object-oriented (Java) basics
Java 8 documentation summary
[Java] Object-oriented summary_Part 2
Java 11 document summary
[Java] Object-oriented syntax-Package
Summary of Docker understanding by beginners ① ~ docker run -p ~
Java 12 new feature summary
[Summary] Java environment preparation
effective java 3rd summary
Java 13 new feature summary
Java static [Personal summary]
Thread safe summary ~ Java ~
Java Primitive Specialization Summary
Java development link summary
Personal summary about Java
Java 10 new feature summary
java regular expression summary
Java 14 new feature summary
Summary of Java support 2018
Java design pattern summary
Java reserved word summary
Java8 Stream Rough Summary
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
Summary of revisions (new era) support by Java version
What is Java Assertion? Summary.
What is object-oriented programming? ~ Beginners ~
[Summary] Why make it object-oriented?
[Java] Beginner's understanding of Servlet-②
[Java11] Stream Summary -Advantages of Stream-
Progate Java (Beginner) Review & Summary
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
[Java] Beginner's understanding of Servlet-①
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
Object-oriented (java) with Strike Gundam
Java8 stream, lambda expression summary
[For beginners] About the JavaScript syntax explained by Java Gold
[Java] Basic summary of Java not covered by Progate ~ Part 2 · List ~
Java 3 major elements (object-oriented) memorandum
Summary of Java language basics
Java tips --Spring execution Summary
[Java] Summary of for statements
Summary of Java Math class
Java for beginners, data hiding
Summary of values returned by the Spliterator characteristics method #java
Java beginners read Hello World
[Java11] Stream Usage Summary -Basics-
[Java] Summary of control syntax