About classes and instances (evolution)

How I wrote this article

In the last post

Difference between class and instance

I wrote an article.

As an example, I made a car as a class and created a car instance with the name "Benz" as an instance, but I received a comment from the person who read the article that the explanation was rough, so I wrote the previous content I will write a covered article. (The previous article has been stripped down to make it easier for beginners to imagine classes and instances.)

Which part of the explanation was rough in the first place?

Here is the part you pointed out.

Each instance has different characteristics. If you make a black car, some of them will be Toyota cars, some will be Mercedes-Benz cars, they will be different in size and shape, and no instance will be the same.

In this regard,

In the first place, Toyota has a blueprint of Toyota, and Benz has a blueprint of Benz, and it is rough to put them together in one Car class and make a car of Benz or Toyota from there. For example, in the case of Toyota Prius, the appearance is the same, but the instances are different in mileage, remaining amount of gasoline, etc., and even if they have the same appearance (same class, same type), the characteristics are different for each instance. This is the difference for each instance.

It was that. I thought that was true this time

--Class inheritance --Class variables --Instance variables

While touching on these three, it is more advanced than the previous article Aim to explain ** classes ** and ** instances **

Class inheritance

Classes have the idea of class inheritance. If you rewrite the class part into a blueprint, it becomes "** inheritance of the blueprint **". Taking the previous article as an example, if you create a [Car2] blueprint (Car2 class) that inherits the [Car] blueprint (Car class), [Car2] The blueprint is

-[Car] What is defined in the blueprint -[Car2] What is defined in the blueprint

Both can be used.

For example, if the above [Car2] blueprint (Car2 class) is used as the [Nissan] blueprint (Nissan car class), ** Nissan car class is automatically inherited by the idea of inheritance without writing the contents defined in Car in the class (blueprint) **

This time, we will define it in advance.

--Car class --NissanCar_serena class

Is created so that the NissanCar_serena class inherits from the Car class. *** Car can run on the ground with a four-wheeled vehicle ** We will proceed on the premise that there is a common understanding.


alt


Let's create a blueprint (class)

Car class

Car.java


class Car {
    static Boolean isHaving4tires = true; ///① Absolutely the car has four tires

    static Boolean canDriveOnGround = true; ///② The car will definitely run on the ground

    String name; ///③ Each instance has a different name

    String color; ///④ Each instance has a different color

    Boolean isHavingWindow; ///⑤ Each instance may or may not have a window.

    public void setNameAndColor(String n, String c) { 
        this.name = n;  ///⑥ I will set the name and color for the instance
        this.color = c;
    }

    public void checkWindow(Boolean b) { 
        this.isHavingWindow = b  ///⑦ I will set whether the instance has a window or not.
    }
}

Contents of Car class (Car blueprint) definition

(1) I created a Boolean type variable that has 4 tires and decided that it is absolutely true. (2) I created a Boolean type variable that can run on the ground, and decided that it is absolutely true. (3) The name that can be changed for each instance is defined by a String type variable. (4) The color that can change for each instance is defined by a String type variable. (5) The information "whether there is a window or not" that can change for each instance is defined by a Boolean type variable. (6) A method for setting the name and color for each instance has been defined. (7) We have defined a method for setting the information "whether there is a window" for each instance.

NissanCar_serena class

Car.java


class NissanCar extends Car {
    
    static String whichMadeByCompany = "Nissan"; ///①絶対にNissanで作成された車です

    String whereBought; ///(2) It has information about "where it was purchased" that can be different for each instance.
    
    public void setWhereBought(String w) { 
        this.whereBought = w;  ///③ Set the instance where it was purchased.
    }
}

Contents of the definition of NissanCar_serena class (NissanCar_serena blueprint)

(1) Defined where it was created with a String type variable, and decided that it was absolutely Nissan. (2) The information "where was purchased" that can change for each instance is defined by a String type variable. (3) A method for setting the name and color for each instance has been defined.

This completes the creation of the blueprint (class).

Class variables

I will explain what is called a class variable while using the above blueprint (class). What is a class variable?

-** The contents of the variable do not change depending on the instance ** -** Common information within the class **

is

Therefore, the class variables referred to in the above class (blueprint) are

In Car class

--static Boolean isHaving4tires = true; (having 4 tires means different instances, but not different) --static Boolean canDriveOnGround = true; (Running on the ground means different instances but no change)

In the NissanCar_serena class

--static String whichMadeByCompany = "Nissan"; (Which company created it does not change even though the instance is different)

If you want to get information about class variables

Class name. Class variable name You can get the value of the class variable by doing.

Let's get the value of a class variable using the above class (1) I want to get the value of the class variable ʻisHaving4tires` in the Car class.

Car.isHaving4tires //true NissanCar_serena.isHaving4tires //true → NissanCar_serena is the inheritance destination of the Car class

(2) I want to get the value of the class variable whichMadeByCompany in the NissanCar_serena class.

NissanCar_serena.whichMadeByCompany //" Nissan "

Instance variables

What is an instance variable?

-** Variables whose values may vary depending on the instance **

is. Therefore, the instance variable referred to in the above class (blueprint) is

In Car class

--String name; (have different names for each instance) --String color; (can have different colors for each instance) --Boolean isHavingWindow (varies whether each instance has a window or not)

In the NissanCar_serena class

--String whereBought; (Information about where the purchase was made may differ for each instance)

If you want to get information about instance variables

Instance.variable name

Let's get the value of the instance variable using the above class

(1) Create an instance of Car class and NissanCar_serena class, set the name and color (Mike's car, red), and get the instance variables name, color.

test.java


  
   void test() {
   
   Car myCar = new Car();
   myCar.setNameAndColor("Mike's car","Red");
   System.out.print(myCar.name); //Access the instance myCar with the instance variable name"Mike's car"Get
   System.out.print(myCar.color); //Access the instance myCar with the instance variable color"Red"Get

   NissanCar_serena myCar2 = new NissanCar_serena();
   myCar2.setNameAndColor("Mike's car","Red");
   System.out.print(myCar2.name);//Access the instance myCar2 with the instance variable name"Mike's car"Get
   System.out.print(myCar2.color);//Access the instance myCar2 with the instance variable color"Red"Get

  }
}

(2) Create an instance of the NissanCar_serena class, set the information of where you bought it, and get the instance variable whereBought.

test.java


  
   void test2() {

   NissanCar_serena myCar2 = new NissanCar_serena();
   myCar2.setWhereBought("Tokyo");
   System.out.print(myCar2.whereBought);//Access the instance myCar2 with the instance variable wherebought"Tokyo"Get
  }
}

Finally

As I wrote this article, I was struck by the ** instance variables of the NissanCar_serena class that I inherited from **.

As explained above

** Instance variables are different for each instance ** ** Class variables are different for each class **

is.

When I tried to define an instance variable, when I imagined the difference between instances in the NissanCar_serena class, which is not in Car, NissanCar is a commercial car called Nissan, while Car class is still commercial or not. Since it is unknown, we will use the difference to define where it was bought,

public String whereBought = "";

Was defined. The possibility is very small, but there is a possibility that people who build a car from scratch and use it for themselves (laugh) However, if you have a class as a blueprint and you are asked if you need to define "where it was purchased" information as its variable, you can't say anything. .. ..

Also defined in the Car class

public Boolean isHavingWindow;

Regarding the instance variable, I would like you to imagine the following image.

alt

Thank you for browsing. I would appreciate it if you could comment if you have any.

Recommended Posts

About classes and instances (evolution)
About classes and instances
Consideration about classes and instances
About Ruby classes and instances
Ruby classes and instances
java (classes and instances)
Getting Started with Java_Chapter 8_About Instances and Classes
Creating Ruby classes and instances
About the difference between classes and instances in Ruby
Writing code with classes and instances
Organize classes, instances, and instance variables
Classes and instances Java for beginners
Java programming (classes and instances, main methods)
About standard classes
Memorandum (Ruby: Basic Grammar: Classes and Instances)
Write code using Ruby classes and instances
I compared classes and instances with worldly things
java classes, instances, objects
HashMap and HashSet classes
String literals and instances
About Bean and DI
[Java] About anonymous classes
About gets and gets.chomp
About redirect and forward
About encapsulation and inheritance
List and happy classes
About Serializable and serialVersionUID
Java classes and instances to understand in the figure
[For beginners] Explanation of classes, instances, and statics in Java
About for statement and if statement
[Java] Generics classes and generics methods
About Ruby hashes and symbols
[Ruby] Creating code using the concept of classes and instances
[Java] About String and StringBuilder
About the same and equivalent
About pluck and ids methods
About Java Packages and imports
About Ruby and object model
[Ruby] Singular methods and singular classes
About abstract classes in java
Ruby methods and classes (basic)
About instance variables and attr_ *
About self-introduction and common errors
Java abstract methods and classes
[Ruby] Singular methods and singular classes
About Java static and non-static methods
Learn more about gems and bundlers
About the equals () and hashcode () methods
[Java beginner] About abstraction and interface
About Ruby single quotes and double quotes
[Ruby] Classes, instance variables, instances, etc ...
About Gradle's setup phase and execution phase
How to call classes and methods
About Ruby product operator (&) and sum operator (|)
About go get and go install from Go1.16
About removeAll and retainAll of ArrayList
Java generics (defines classes and methods)
About if statement and branch processing
About object-oriented inheritance and about yield Ruby
About Java primitive types and reference types
Consideration about Rails and Clean Architecture