[Java] [Java basics] What is Class

6 minute read

Introduction

This article is friendly Java I wrote it for the purpose of organizing the knowledge I learned by myself with the reference book.

What is Class

A collection of states, properties, and functions related to general items

  • field: A mechanism that represents the “state/property” of the class
  • method: Mechanism that represents the “function” of the class
  • member: field, method…etc

Class declaration

Declare the class as follows:

``


class class name
{
    Type name field name;
    ...
    Return type Method name (argument list)
    {
    Sentence;
・・・
Return expression;
    }
    ...
}

Use Class

Just by declaring a class, you don’t know in detail what it is. What you need is to create an object.

As an example, let’s replace it with a car.

``


// 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 “gas amount”. After that, all you have to do is write the code to make one car based on the declared class.

  • Each car created on the code like this is called object/instance.

``


// 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 + ".");
        System.out.println("Gas volume is "+ car1.gas + ".");
    }
}
  • Once you create an object, you can access its members

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

Constructor

In addition to fields and methods, you can write a constructor in a class.

Constructors are very similar to methods, but

  • The name of the constructor is always the same as the class name
  • Cannot specify return value
  • When the object of that class is created, the processing in the defined constructor is automatically performed.

There is a difference such as.

``


class Car
{
private int num;
private double gas;

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

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

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

Below, execution result

``


I created a car.
The car number is 0.
The amount of gasoline is 0.0.

Class features

Encapsulation

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

Example) Field → private member, method → public member

This allows you to create programs that are less prone to errors.

  • public: Can be accessed from outside the class
  • private: Inaccessible from outside the class
  • Neither of them (Omitted): Can be accessed from within the class included in the “same package”

Overload

Even within the same class, it should be possible to define methods with the same method name but different argument types and numbers. *Constructor can be overloaded

``


class Car
{
private int num;
private double gas;

public void setCar(int n)
{
num = n;
System.out.println("The number is changed to "+ num + ".");
}

public void setCar(double g)
{
gas = g;
System.out.println("The amount of gasoline is set to "+ gas + ".");
}

public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("The number is set to "+ num + "and the amount of gasoline is set to "+ gas + ".");
}

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

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

System.out.println("Change only car number.");
car1.setCar(2345);
car1.show();

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

The output result is as follows.

``


The number is 1234 and the gasoline amount is 20.5.
The car number is 1234.
The amount of gasoline is 20.5.
Change only the car number.
I changed the number to 2345.
The car number is 2345.
The amount of gasoline is 20.5.
Change only the amount of gasoline.
The amount of gasoline was set to 30.5.
The car number is 2345.
The amount of gasoline is 30.5.

In the above code, three types of setCar() methods are called.

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

You can use the same method name for similar processing, which is very convenient.

*Note, however, that you cannot overload unless the argument types or number differ.

inheritance

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

  • Creating a new class is called extends.

Taking a car as an example,

``


class car
{
    number;
    Amount of gasoline;
    Ability to display number and gasoline volume
}

class racing car extends car
{
    Competition course;
    Ability to display competition courses;
}
  • Parent class “Car” is a super class
  • Subclass of added class “Racing Car”

The subclass inherits the members of the superclass.

*Note: Members of multiple superclasses cannot be inherited by one subclass.

Access to members

Subclass private members are also inaccessible to subclasses.

Therefore, you can specify protected in the superclass.

  • protected: In addition to subclasses, it can be accessed by classes belonging to the same package.

Override

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

``


class Car
{
protected int num;
protected double gas;

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

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

public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("The number is set to "+ num + "and the amount of gasoline is set to "+ gas + ".");
}

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

class RacingCar extends Car
{
private int course;

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

public void setCourse(int c)
{
course = c;
System.out.println("The course number has been changed to "+ course + ".");
}

public void show()
{
System.out.println("Racing car number is "+ num + ".");
System.out.println("Gas volume is "+ gas + ".");
System.out.println("The course number is "+ course + ".");
}
}

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

``


I created a car.
I created a racing car.
The number is 1234 and the gasoline amount is 20.5.
I changed the course number to 5.
The racing car number is 1234.
The amount of gasoline is 20.5.
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, it seems that the method newly defined in the subclass is called.

final

You can avoid overriding by adding final at the beginning of the superclass method.

Reference

Tags:

Updated: