[Beginner] Points to be aware of after Java exercises / Inheritance / Abstract method [Note 26]

Java practice (object oriented)

Encapsulation

When writing an instance field with "private" Since it may be missed by the getter and an error has occurred Be careful to write in a habit.

printData -Even if there is no instruction, you can create an instance method for output by yourself. Be able to write. I want to keep in mind the writing style that is easy for anyone to see.

-After writing in private, instead of outputting in Main.java When grouping the output with printData, it is in the same class, so instead of getName etc. If you write it with this.name etc., you can write it concisely, so remember it.

Java learning

Inheritance

I used to use two classes in the exercises so far There was some code duplication in the class. Duplication should be avoided as it will be difficult to correct and improve.

At that time, a method of inheriting the contents of one class can be taken. This is called inheritance.

The original class is called a "super class" The inherited class is called a "subclass".

How to inherit

When first defining in a subclass

class subclass name extends superclass name{...

Write in the form of.

class Vehicle{
 ...
 public void setName(String name){...
 }
 public void setColor(String color){...
 }
}

//Main.in java
class Main{
 public static void main(String[]args){
  Car car = new Car();
  car.setName("Ferrari");
  car.setColor("Red");
...
}
}

When actually calling a method, write as above. (One case)

Customize subclasses

The fuel part of the Car class used so far is not in other classes Since it is a unique product, it must be customized in the Car class.

class Car extends Vehicle{
 private int fuel = 50;
 public int getFuel(){
  return this.fuel;
 }

When actually calling it, call it with car.getFuel () ;. (vehicle.getFuel (); gives an error)

super() How to call a superclass constructor in a subclass [Example 1]

//Super class
class Vehicle{
 Vehicle(){
  System.out.println("Super class");
 }

//Car class
class Car extends Vehicle{
 Car(){
 super();
 }
}

//Main class
class Main{
 public static void main(String[]args){
  Car car = new Car();
  ...
 }
}

In the above, the output will be "superclass". [Example 2]

//First, define the constructor in the superclass.
class Vehicle{
 private String name; ...

 Vehicle(String name,String color){
  this.name = name;
  this.color = color;
 }

//Call the Car class constructor / superclass constructor
class Car extends Vehicle{
 Car(String name,String color){
  super(name,color); //Don't forget to pass arguments.
 }
}

protected When using superclasses and subclasses For fields that use private as before Access from subclasses cannot be done as it is. I used to use getters and setters, but when I use [protected] Allows access from subclasses and within the class.

・ Public → Can be accessed from anywhere ・ Private → Only accessible from that class · Protected → accessible from that class and subclass

Abstract method

Until now, subclasses of cars and bicycles in the superclass of vehicles I summarized it, but when there are methods with different contents (for example, run class) Use abstract methods.

class Vehicle{ ..
 abstract public void run(int distance);
//Here we define the abstract method. I will not write the processing after that!

After that, overwriting is required in each subclass.

from now on

I don't actually write abstract methods, so I feel that I don't understand much. I would like to review Java once and create it as a page. For various reasons, it is better to touch PHP as soon as possible, so before that, let's move on to learning PHP.

Recommended Posts

[Beginner] Points to be aware of after Java exercises / Inheritance / Abstract method [Note 26]
Things to be aware of when writing Java
[Java] Things to be aware of when outputting FizzBuzz
[Java Silver] Things to be aware of regarding switch statements
Things to be aware of when writing code in Java
[Java] Points to note with Arrays.asList ()
To be aware of easy-to-read code
[Beginner] Java method / class / external library [Note 23]
Summarize the life cycle of Java objects to be aware of in Android development
[Java] Be aware of short circuits (short-circuit evaluation)
Java Servlet should be aware of multithreaded environment
Java Beginner Exercises
[Java] How to use compareTo method of Date class
[Beginner] Java class field method / encapsulation (getter setter) [Note 25]
Basic rules to be aware of to write easy-to-read code
[Beginner] Java Object Oriented / Instance Field / Instance Method / Overload [Note 24]
Things to be aware of when using devise's lockable
I want to be aware of the contents of variables!
Java abstract modifier [Note]
Setting method to link Java of Eclipse and Github / September 2017
Be sure to compare the result of Java compareTo with 0
[Java beginner] Conversion from character string to numerical value-What is the parseInt method of the Integer class? ~