Java for beginners, data hiding

Postscript: 2020/3/13 Since the comment pointed out that the content of this time was "data hiding" rather than "encapsulation", it was corrected. The title has also changed from encapsulation to data hiding.

Introduction

This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ~~ ** The mistaken ** is the center. This is for the purpose of posting the part that was actually mistaken during coding and posting it for self-reflection. ~~ In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.

environment

Language: Java11, JDK13.0.2 Operating environment: Windows 10

Restrict access to members

I confirmed that the instance created by new has its own members (fields and methods) inherited from the class. But in the last Cat class

Cat myCat = new Cat; mycat.weight = -20;

And, the value that was not expected when the Cat class was created can be stored arbitrarily from within the main method. Allowing access to the ** field (this time double weight) ** set in the Cat class directly from within the main method can cause problems.

private Therefore, when "I want to have a member (field method) that cannot be handled from outside the class processing", the qualifier attached to the member (field method) is ** private **.

This time I would like to handle the Cat class by changing from the String type color to the double type height.

classCat re-challenge


class Cat
{
  private double weight;
  private double height;
  //class

  public void setWeightHeight(double w,double h)
  {
    if(w > 0 && w <= 50){
    weight = w;
    System.out.println("The weight of this cat" + weight + "I made it kg.");
    } else{
        System.out.println("I don't think I have this weight ...");
      }
   
    if(h > 0 && h <= 200){
    height = h;
    System.out.println("The height of this cat" + height + "I made it cm.");
    } else{
    System.out.println("I've never heard of a cat of that height ...");
      }
  }

  double getWeight()
  {
    System.out.println("Update the weight of the cat.");
    return weight;
  }

  double getHeight()
  {
    System.out.println("Update the height of the cat.");
    return height;
  }

  void introduce()
  {
    System.out.println("The weight of this cat" + weight + "It is kg.");
    System.out.println("The height of this cat" + height + "cm.");
  }
}

Since the weight height of the Cat class is now private, there is no need to worry about the value being tampered with.

public But if you really can't change the value, you can't change the value of the new myCat instance you want to new. But in the Cat class

public void setWeightHeight(double w,double h)

I want you to pay attention to. This is a method defined in the Cat class, but its qualifier is set to ** public **. This is a modifier that "allows access from outside the class" and is part of the process

weight = w; height = h;

And ** accessing the field set to ** private **. Of the methods, using the fact that private can be accessed from methods that are in the same class, ** Storage to the value set in private via in-method processing is possible. Allowed **.

public void setWeightHeight(double w,double h) Has an if statement inside it, which can prevent the assignment of values that do not meet the conditions. Now you don't have to worry about storing ridiculous values.

At the end

About the process I saw this time, "Use a method to store a value in the private field. " Quoting from the reference book Easy Java 7th Edition,

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.

Is. This functional design ** ~~ encapsulation ~~ data hiding ** I call it. This is ** one of the basic principles of object-oriented design **, and classes are created for this purpose. I will deal with other principles again, but they are all important principles, so I want to grasp them firmly.

reference

I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.

Easy Java 7th Edition Java SE11 Silver Problem Collection (commonly known as Kuromoto)

Recommended Posts

Java for beginners, data hiding
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
Java application for beginners: stream
[For beginners] Summary of java constructor
Rock-paper-scissors game for beginners in Java
Java for beginners, expressions and operators 1
[For beginners] Run Selenium in Java
Java for beginners, expressions and operators 2
[For Java beginners] About exception handling
Classes and instances Java for beginners
For JAVA learning (2018-03-16-01)
Learn Java with "So What" [For beginners]
2017 IDE for Java
Java for statement
Kantai Collection Java # 1 Classes and Objects [For Beginners]
A collection of simple questions for Java beginners
[Introduction to Java] Basics of java arithmetic (for beginners)
Let's use Java New FileIO! (Introduction, for beginners)
[Java] for statement, while statement
[Java] Package for management
[Java] for statement / extended for statement
[Java] Data type ①-Basic type
Countermeasures for Java OutOfMemoryError
NLP for Java (NLP4J) (2)
(Memo) Java for statement
NLP for Java (NLP4J) (1)
[Java] Main data types
Java basic data types
Scraping for beginners (Ruby)
[For beginners] Quickly understand the basics of Java 8 Lambda
[For beginners] How to operate Stream API after Java 8
Generate dummy data for various tests with Faker (java)
[For beginners] Minimum sample to display RecyclerView in Java
Java development for beginners to start from 1-Vol.1-eclipse setup
Introduction to Java for beginners Basic knowledge of Java language ①
List of frequently used Java instructions (for beginners and beginners)
[For beginners] ○○. △△ in Ruby (ActiveRecord method, instance method, data acquisition)
[For beginners] Explanation of classes, instances, and statics in Java
[Java Bronze learning] Differences between encapsulation, data hiding, and information hiding
Java learning memo (data type)
Java update for Scala users
[Java] Beginner's understanding of Servlet-②
Java programming (variables and data)
[Java] Precautions for type conversion
Books used for learning Java
[Java] Beginner's understanding of Servlet-①
Importing Excel data in Java 2
2018 Java Proficiency Test for Newcomers-Basics-
(For beginners) [Rails] Install Devise
Import Excel data in Java
[For beginners] About the JavaScript syntax explained by Java Gold
[For super beginners] Ant super introduction
More usable Enumerable for beginners
Java thread safe for you
[Java] Summary of for statements
[Java] Tips for writing source
Importing Excel data in Java 3
Java beginners read Hello World
Java installation location for mac
Introducing Spring Boot2, a Java framework for web development (for beginners)