[Java basics] What is Class?

Introduction

This article is about [Easy Java](https://www.amazon.co.jp/%E3%82%84%E3%81%95%E3%81%97%E3%81%84Java-%E7%AC % AC7% E7% 89% 88-% E3% 80% 8C% E3% 82% 84% E3% 81% 95% E3% 81% 97% E3% 81% 84% E3% 80% 8D% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA-% E9% AB% 98% E6% A9% 8B-% E9% BA% BB% E5% A5% 88 / dp / 4815600848) I wrote it for the purpose of organizing the knowledge I learned by myself in the reference book.

What is Class

A summary of states, properties, and functions related to things in general

--field: A mechanism to represent the "state / property" of a class --method: A mechanism to represent the "function" of a class --member: field, method ... etc

Class declaration

The class is declared as follows:

python


class class name
{
Type name Field name;
・ ・ ・
Return type method name(Argument list)
    {
Sentence;
・ ・ ・
return expression;
    }
・ ・ ・
}

Use of Class

Just declaring a class doesn't tell you in detail what it is. So what you need to do is create an object.

As an example, let's replace it with "car".

python


//Car class
class Car
{
    //field
    int num; //Car number
    double gas; //Amount of gasoline
}

At the above stage, you can see that the thing called "car" has "number" and "amount of gasoline". After that, based on the declared class, just write the code that you want to make one car.

――In this way, each car created on the code is called object / instance.

python


//Create a car class object
class Sample1
{
    public static void main(String[] args)
    {
        Car car1 = new Car();

        car1.num = 1234;
        car1.gas = 20.5;

        System.out.println("The car number is" + car1.num + "is.");
        System.out.println("The amount of gasoline" + car1.gas + "is.");
    }
}

--You can access the members by creating an object

** To use a class, declare the class and create an object. ** **

Constructor In addition to fields and methods, you can write constructors in your class.

Constructors are very similar to methods, but

--The constructor name is always the same as the class name --Return value cannot be specified --When an object of that class is created, the processing in the defined constructor is automatically performed.

There is a difference.

python


class Car
{
	private int num;
	private double gas;

	// constructor
	public Car()
	{
		num = 0;
		gas = 0.0;
		System.out.println("I made a car.");
	}

	public void show()
	{
		System.out.println("The car number is" + num + "is.");
		System.out.println("The amount of gasoline" + gas + "is.");
	}
}

class Sample
{
	public static void main(String[] args)
	{
		Car car1 = new Car();
		car1.show();
	}
}

Below, execution result

python


I made a car.
The car number is 0.
Gasoline amount is 0.It is 0.

Function of Class

Encapsulation

A function that puts data (fields) and functions (methods) together in a class and attaches private to the members you want to protect so that they cannot be accessed without permission.

Example) Field → private member, method → public member

This makes it possible to create programs that are less prone to errors.

--public: Can be accessed from outside the class --private: Cannot be accessed from outside the class --Neither (omitted): Can be accessed from within the class included in the "same package"

Overload

Even within the same class, it is possible to define methods with the same method name but different argument types and numbers.

python


class Car
{
	private int num;
	private double gas;

	public void setCar(int n)
	{
		num = n;
		System.out.println("Number" + num + "I made it.");
	}

	public void setCar(double g)
	{
		gas = g;
		System.out.println("Gasoline amount" + gas + "I made it.");
	}

	public void setCar(int n, double g)
	{
		num = n;
		gas = g;
		System.out.println("Number" + num + "Gasoline amount" + gas + "I made it.");
	}

	public void show()
	{
		System.out.println("The car number is" + num + "is.");
		System.out.println("The amount of gasoline" + gas + "is.");
	}
}

class Sample
{
	public static void main(String[] args)
	{
		Car car1 = new Car();
		car1.setCar(1234, 20.5);
		car1.show();

		System.out.println("Only change the car number.");
		car1.setCar(2345);
		car1.show();

		System.out.println("Change only the amount of gasoline.");
		car1.setCar(30.5);
		car1.show();
	}
}

The output result is as follows.

python


Number 1234 and gasoline amount 20.I set it to 5.
The car number is 1234.
The amount of gasoline is 20.It is 5.
Only change the car number.
I changed the number to 2345.
The car number is 2345.
The amount of gasoline is 20.It is 5.
Change only the amount of gasoline.
30 gasoline.I set it to 5.
The car number is 2345.
The amount of gasoline is 30.It is 5.

The above code calls three types of setCar () methods.

--First, the one with two arguments --Second, the argument is of type int --Third, the argument is double type

It is very convenient because you can use the same method name for similar processing. * However, note that overloading is not possible unless the argument types or numbers are different! </ font>

Inheritance

In Java, you can create a new class based on the class you have already created.

--Creating a new class is called extends.

Taking a car as an example, it looks like this:

python


class car
{
number;
Gasoline amount;
Function to display number and amount of gasoline
}

class racing car extends car
{
Competition course;
Function to display the competition course;
}

--Super class of the parent class "car" --Subclass the added class "Racing Car"

Subclasses inherit members of superclasses.

* Note: Members of multiple superclasses cannot be inherited by one subclass. </ font>

Access to members

Private members of superclasses are also inaccessible to subclasses.

Therefore, in superclasses, you can specify protected.

--protected: Can be accessed by subclasses as well as classes belonging to the same package.

override

You can define a method that has exactly the same method name, number of arguments, and type as the superclass.

python


class Car
{
	protected int num;
	protected double gas;

	public Car()
	{
		num = 0;
		gas = 0.0;
		System.out.println("I made a car.");
	}

	public Car(int n, double g)
	{
		num = n;
		gas = g;
		System.out.println("number" + num + "Gasoline amount" + gas + "Created a car.");
	}

	public void setCar(int n, double g)
	{
		num = n;
		gas = g;
		System.out.println("Number" + num + "Gasoline amount" + gas + "I made it.");
	}

	public void show()
	{
		System.out.println("The car number is" + num + "is.");
		System.out.println("The amount of gasoline" + gas + "is.");
	}
}

class RacingCar extends Car
{
	private int course;

	public RacingCar_11()
	{
		course = 0;
		System.out.println("I made a racing car.");
	}

	public void setCourse(int c)
	{
		course = c;
		System.out.println("Course number" + course + "I made it.");
	}

	public void show()
	{
		System.out.println("The number of the racing car is" + num + "is.");
		System.out.println("The amount of gasoline" + gas + "is.");
		System.out.println("The course number is" + course + "is.");
	}
}

class Sample
{
	public static void main(String[] args)
	{
		RacingCar rccar1 = new RacingCar();
		rccar1.setCar(1234, 20.5);
		rccar1.setCourse(5);
		rccar1.newShow();
	}
}

Below, execution result

python


I made a car.
I made a racing car.
Number 1234 and gasoline amount 20.I set it to 5.
I changed the course number to 5.
The number of the racing car is 1234.
The amount of gasoline is 20.It is 5.
The course number is 5.

When I created a subclass object and called the show () method, the subclass's show () method was called.

* If the method name, number of arguments, and type are exactly the same, the method newly defined in the subclass seems to be called. </ font>

final You can avoid overriding by prefixing the superclass method with final.

reference

-[Easy Java](https://www.amazon.co.jp/%E3%82%84%E3%81%95%E3%81%97%E3%81%84Java-%E7%AC%AC7% E7% 89% 88-% E3% 80% 8C% E3% 82% 84% E3% 81% 95% E3% 81% 97% E3% 81% 84% E3% 80% 8D% E3% 82% B7% E3 % 83% AA% E3% 83% BC% E3% 82% BA-% E9% AB% 98% E6% A9% 8B-% E9% BA% BB% E5% A5% 88 / dp / 4815600848)

Recommended Posts