[JAVA] Object-oriented course for beginners sent by beginners

Talk about doing Advent Calendar on Qiita

We will have an Advent Calendar at Qiita in the middle of summer, so an out-of-season project has started. The period will be from 8/12 to 9/12, so let's do our best. First of all, I will decorate the first time, I thought about what to write for a day, but since I am still a beginner and I do not write much, even the object-oriented article for beginners sent by beginners is fluttering I think I'll write.

Let's go to the main subject

So, let's finish the simple promotion and get into the main subject. ** Please refer to the git that lists the source files in the summary. ** **

Object-oriented course for beginners sent by beginners

First of all, what is object-oriented? I think there are people. I hadn't actually touched object-oriented programming until about two months ago, so I was a person on that side, so I will talk only with the knowledge I have cultivated in about a month, so please forgive me. I will. And in this article, I will write ** (only Java can be written) ** articles using Java, an object-oriented language.

That's why it's object-oriented.

Object-oriented is often compared to Taiyaki molds and Taiyaki. A class is likened to a Taiyaki type, and an instance is likened to Taiyaki. Such an analogy is used to create an instance by duplicating it into a type called a class.

But I don't think such a mundane metaphor is interesting, *** Bye, you're talking about yourself, right? *** ***

Let's compare it ..... (3 hours later)

Object-oriented is ... a coalesced robot. But it's not just a united robot, it's a united robot that allows you to customize parts. And if you compare it like the example of Taiyaki, the instance is a united robot part and the robot factory is a class. The reason why you can customize parts is that you can create parts (classes) by yourself in object-oriented programming. However, I think that you usually do not make your own Taiyaki mold. Then, combine various instances (arms and legs) into Main.java (torso). Java has three important elements, which we will talk about.

What are the three major elements of Java?

The three major elements of Java are 1. inheritance, 2. polymorphism, and 3. encapsulation. Let's talk lightly about them.

1. Inheritance

Inheritance means that you can access the methods and variables of a super class (parent class, inherited class) from a subclass (child class, inherited class) by inheriting one class to another class. The reason why it is convenient to inherit is, for example, to create a figure class (Figure), but there are rectangles, triangles, and circles, and there are rectangle class (Rectangle), triangle class (Triangle), and circle class (Circle). Each class has similar contents. For example, when a figure has a color, a rectangle, a height, and a base, and the area can be calculated, but the calculation formula is different for each, so it cannot be unified. By inheriting each figure, it is not necessary to describe common information such as colors one by one, and reusability is improved. You can then override the method by defining a method with the same name as the superclass in the subclass. By overriding, you can fill in the subtle differences in each subclass. As mentioned in the example above, the calculation formulas are different for Rectangle, Triangle and Circle. However, it is possible to define the calcArea method in the figure, override it in each subclass, and divide the calculation formula by the method with the same name. IMG_0123.jpg

2. Polymorphism

Polymorphism is the ability to instantiate an inherited subclass as a superclass. The advantage of creating an instance in a superclass is that it can be stored in the same array or ArrayList because it is the same type. But it's not just about the benefits. Instead, you can't call methods that aren't in the superclass. However, it can be called from a method in the superclass. So, a common person such as creating an outline (general-purpose part), inheriting it, deriving it to each character, etc., overriding the method to solidify the details, creating an instance in the superclass, and combining the characters into one array. You can do various operations with.

IMG_0124.jpg

Encapsulation

Encapsulation is to declare a local variable as private and use methods called setters and getters to assign and retrieve the target. The reason why the method is used to interfere with the annoying value is to prevent errors caused by unintentional assignment by interfering with the value in one step.

Summary

Object-oriented programming is still deep, but since it is an object-oriented course where beginners come to beginners, this time I explained the three major elements of Java. I would like to draw a more understandable explanation when I become a person who can do more. git:https://github.com/kindainoob/Figure.git

At the very end

I will write an article about making a One Night Werewolf separately from the Advent Calendar later in several articles. Thank you for your cooperation. .. ..

Recommended Posts

Object-oriented course for beginners sent by beginners
Object-oriented summary by beginners (Java)
Array variables and associative arrays compiled by beginners for beginners
Scraping for beginners (Ruby)
[For beginners] About the JavaScript syntax explained by Java Gold
What is object-oriented programming? ~ Beginners ~
[Java] Basic statement for beginners
Knowledge required for object-oriented refactoring
[For super beginners] DBUnit super introduction
(For beginners) [Rails] Install Devise
[For super beginners] Ant super introduction
More usable Enumerable for beginners
Java for beginners, data hiding
[For super beginners] Maven super introduction
Java application for beginners: stream
[RSpec on Rails] How to write test code for beginners by beginners