Summary of object-oriented programming using Java

Summary of object-oriented programming using Java

I thought that the material I made for the study session was "well, I was able to make a decent amount for myself," so I posted this for the first time. However, it would be very embarrassing if I wrote the wrong content with this.

What is a class

In object-oriented programming, a unit of 1 with behavior and attribute is called object, and this object type is called class. To break the story a little more,

Is.

The standard syntax of the class is as follows.

class sandbox {
  public int field;
  public void method() {
    // Write anything process.
  }
}

Basic class usage

Instantiation

__ Creating a class object __. Also known as " new ". Instantiation is performed as follows. By instantiating, you can use the members and methods of the instantiated class.

class sandbox {
  public static void main(String[] args) {
    Class cls = new Class(); // Create instance.
    cls.method(); // You can action method() of  Class.
  }
}

class Class {
  public int field;
  public void method() {
    // Write anything process.
  }
}

constructor

A constructor is a method that is executed only once when it is instantiated. When creating the constructor, the method name should be the same as the class name.

class sandbox {
  public static void main(String[] args) {
    Class cls = new Class();
    // Create instance and action constructor.
  }
}

class Class {
  public int field;
  public Class() {
    System.out.println("Action constructor.");
  }
  public void method() {
    // Write anything process.
  }
}

Execution result

Action constructor.

Modifier

1. Access modifier

Access from the outside can be restricted by adding an access modifier.

Access modifier Access range
public all
protected In the same package, own class, subclass
None In the same package / own class
private Own class

A simple access example is as follows.

class sandbox {
  public static void main(String[] args) {
    Class cls = new Class(); // Create instance.
    System.out.println("You can access " + cls.public_fld + " field.");
    System.out.println("You can access " + cls.protected_fld + " field.");
    System.out.println("You can access " + cls.nothing_fld + " filed.");

    // Compile err.
    // System.out.println("You can " + cls.private_fld + " field.");
  }
}

class Class {
  public String public_fld = "public";
  protected String protected_fld = "protected";
  String nothing_fld = "nothing";
  private String private_fld = "private";
}

Execution result

You can access public field.
You can access protected field.
You can access nothing field.

When I try to uncomment and compile the last System.out.println ("You can "+ cls.private_mbr +" member ");, I get a compile error due to access restrictions.

2. static modifier

The static qualifier allows it to be expanded in memory at runtime and accessed without instantiation.

class sandbox {
  public static void main(String[] args) {
    System.out.println("You can access " + Class.static_fld + " field.");
  }
}

class Class {
  static String static_fld = "static";
}

Execution result

You can access static field.

3. final modifier

By using the final modifier, it becomes a constant and its value cannot be changed.

class sandbox {
  public static void main(String[] args) {
    System.out.println("You can access " + Class.static_fld + "member.");

    // Compile err.
    // Class.static_fld = "final";
  }
}

class Class {
  static final String static_fld = "static";
}

Encapsulation

Put fields and methods together and add private to the members you want to protect so they cannot be accessed. Encapsulation is basic,

Is often used. These two methods are called __accessor method __.

class sandbox {
  public static void main(String[] args) {
    Class cls = new Class();
    cls.setField("hoge");
    System.out.println(cls.getField());
  }
}

class Class {
  private String field = "";

  /**
   * Accessor method.
   * setField(String param) -> Setter
   * getField() -> Getter
  **/
  void setField(String param) {
    field = param;
  }

  String getField() {
    return field;
  }
}

Execution result

hoge

However, there is a story that it is meaningless to make getter and setter and access the field with private, so pure getter and setter realize encapsulation. Otherwise, I wrote in this article.

By the way, encapsulation also means that you can achieve the purpose without worrying about data and processing (please tell me in the comment, article _reference-867e1b32cdfedfcc02e2) also did it.).

How to use advanced classes

Inheritance

To give a class a __parent-child relationship __. A child class that inherits the parent class will inherit the functionality of the __parent class __.

class sandbox {
  public static void main(String[] args) {
    Child cld = new Child();
    cld.sayCommentByPerson();
    cld.sayCommentByChild();
  }
}

// Super class.
class Person {
  void sayCommentByPerson() {
    System.out.println("I am Person.");
  }
}

// Sub class.
class Child extends Person {
  void sayCommentByChild() {
    System.out.println("I am Child.");
  }
}

Execution result

I am Person.
I am Child.

To call the constructor of the parent class

If your parent has a constructor and you want to call it, you can do super ();.

class sandbox {
  public static void main(String[] args) {
    Child cld = new Child();
    cld.sayCommentByChild();
  }
}

// Super class.
class Person {
  Person() {
    System.out.println("I am Person.");
  }
}

// Sub class.
class Child extends Person {

  //Call constructor of super class.
  Child() {
    super();
  }

  void sayCommentByChild() {
    System.out.println("I am Child.");
  }
}

Execution result

I am Person.
I am Child.

Overload

Overload is __ to generate multiple methods with the same name with different arguments __.

class sandbox {
  public static void main(String[] args) {
    Class strClass = new Class("hoge");
    Class intClass = new Class(1);
  }
}

class Class {
  Class(String param) {
    System.out.println("String: " + param);
  }
  
  Class(int param) {
    System.out.println("Integer: " + param);
  }
}

Execution result

String: hoge
Integer: 1

override

Override is to overwrite the method inherited from the parent class with the child class.

class sandbox {
  public static void main(String[] args) {
    Child cld = new Child();
    cld.sayComment();
  }
}

// Super class.
class Person {
  void sayComment() {
    System.out.println("I am Person.");
  }
}

// Sub class.
class Child extends Person {

  @Override
  void sayComment() {
    System.out.println("I am Child.");
  }
}

Polymorphism

Being able to change the behavior depending on the application by overloading, overriding, and interface (described later).

Abstract class

__ A class that considers only the purpose of the parent class __. Therefore, it cannot be instantiated directly.

class sandbox {
  public static void main(String[] args) {
    // Compile err.
    // Person prson = new Person();

    Child chld = new Child();
    chld.sayCommentByPerson();
    chld.sayCommentByChild();
  }
}

abstract class Person {
  void sayCommentByPerson() {
    System.out.println("I am Person.");
  }
}

class Child extends Person {
  void sayCommentByChild() {
    System.out.println("I am Child.");
  }
}

Execution result

I am Person.
I am Child.

interface

__ A further abstraction of the abstract class __. Methods can be declared in the interface, but processing is not written in principle. I get a compile error when I try to write __ processing normally.

class sandbox {
  public static void main(String[] args) {
    Class cls = new Class();
    cls.sayComment();
  }
}

class Class implements Interface {
  public void sayComment() {
    System.out.println("I am Class.");
  }
}

interface Interface {
  void sayComment();
}

Also, the methods in ʻinterface are treated as public, so when using sayComment () of ʻinterface in Class, you must add public. Also, the members in ʻinterface are treated as public static final`.

When describing processing in ʻinterface`

Add the default qualifier.

class sandbox {
  public static void main(String[] args) {
    Class cls = new Class();
    cls.sayComment();
  }
}

class Class implements Interface {

}

interface Interface {
  default void sayComment() {
    System.out.println("I am Interface.");
  }
}

Execution result

I am Interface.

A class can capture multiple ʻinterface`s

class sandbox {
  public static void main(String[] args) {
    Class cls = new Class();
    cls.sayComment1();
    cls.sayComment2();
  }
}

class Class implements Interface1, Interface2 {

}

interface Interface1 {
  default void sayComment1() {
    System.out.println("I am Interface1.");
  }
}

interface Interface2 {
  default void sayComment2() {
    System.out.println("I am Interface2.");
  }
}

Execution result

I am Interface1.
I am Interface2.

Recommended Posts

Summary of object-oriented programming using Java
Summary of using FragmentArgs
Summary of using DBFlow
Summary of Java support 2018
[Java11] Stream Summary -Advantages of Stream-
Summary of using Butter Knife
[Java] Summary of regular expressions
Object-oriented summary by beginners (Java)
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
Object-oriented summary
Object-oriented programming
Summary of [Java silver study] package
Handling of time zones using Java
[Java] Object-oriented
I tried using GoogleHttpClient of Java
[Java Silver] Summary of access modifier points
Summary of in-house newcomer study session [Java]
Newcomer training using the Web-Basic programming using Java-
Summary of changes other than JEP of Java10
[Java] [Maven3] Summary of how to use Maven3
Status monitoring of java application using Elasticsearch
Introduction of New Generation Java Programming Guide (Java 10)
Acquisition of input contents using Scanner (Java)
Java Summary of frequently searched type conversions
Introduction of New Generation Java Programming Guide (Java 11)
Summary of Java Math.random and import (Calendar)
Introduction of New Generation Java Programming Guide (Java 12)
Java knowledge summary
java programming basics
Java related summary
Object-oriented FizzBuzz (Java)
[Java] Object-oriented summary_Part 1
Object-oriented programming terminology
[Java] Object-oriented syntax-Constructor
Object-oriented (Java) basics
java Generic Programming
Java 8 documentation summary
[Java] Object-oriented summary_Part 2
Java 11 document summary
[Java] Overview of Java
[Java] Object-oriented syntax-Package
[java] Summary of how to handle character strings
Summary of Java environment settings for myself [mac]
[Java] Personal summary of classes and methods (basic)
[Java] Summary of how to abbreviate lambda expressions
Verification of performance impact when using Java volatile
The story of learning Java in the first programming
Try similar search of Image Search using Java SDK [Search]
Example of using addition faster than using StringBuilder (Java)
[Note] Summary of rails login function using devise ①
Story of test automation using Appium [Android / java]
Awareness of object-oriented programming for half a year
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
Expired collection of java
Sorting using java comparator