[Java] Memorandum of new graduate SES [Java object oriented edition]

2 minute read

[Java object oriented edition]

This is a Java object-oriented memo for newcomer training.

Object-oriented (property, method)

It was a concept of componentization, and was devised to overcome “complexity that humans cannot grasp.” image.png

// ①
public class Car{
  String maker
  int displacement
  String color

  void start(){
    System.out.println("start")
  }
  void turn(){
    System.out.println("bend")
  }
  void stop(){
    System.out.println("stop")
  }
}
// ②
public class TestCar{
  public static void main(String[] args) {
    Car car = new Car();
  }
}

What you are doing with object orientation. (Example above) ① Create a car design document ↓ ② Manufacture an instance (new) car based on the car design document.

The constructor is executed immediately after instantiation. First only.

// Basic format of the constructor

public class class name
  name of the class() {
    // Describe the automatic execution process here.
  }
}

The method name and class name of the constructor are the same. No return value in method declaration (no void) The variables declared in the class block are called fields.

[3 major object-oriented functions]

① Encapsulation (access control)

private Only accessible from the same class

Class is private Method is public Qualify the field with private.

Fields are hidden by private and accessed via getter and setter methods. The method protects the field.

getter and setter

//Standard method of getter method
public Type of field to retrieve value get field name() {
  return this. field name;
}

In order to be able to call the fields of my class from other classes, A method that simply returns the contents of the field

// Standard setter method
public void set field name (field type arbitrary variable name) {
  this. field name = any variable name;
}

A method that simply assigns a specified value to a particular field

② Inheritance ([extends] create something similar)

Override means overwriting members of the parent class on the child class side when declaring a child class that inherits the parent class.

Inheritance indicates that there is a specialization/generalization relationship between two classes. Inherited from super class Subclass the inheritance destination

*If you specify final when declaring, the class cannot be inherited.

③ Diversity (roughly the same. The result is efficient development)

Vaguely consider the instance. ‥

Relationship of is-a Child class is-a parent class (child class is a kind of parent class)

① ② ③ Use these to reproduce the virtual world in your computer

Java virtual world, computer memory area

Chat

The object orientation learned in Java training is utilized in my current work. This article was compiled by me, a beginner in programming, so if you have any suggestions, I would be grateful if you could comment!

Outside of work, I use Ruby and Ruby on Rails to create portfolios. I will continue to do my best in the future.