[JAVA] About encapsulation and inheritance

We will summarize the encapsulation and inheritance of the three major object-oriented functions.

Purpose of encapsulation

Encapsulation is a function that restricts reading and writing to fields and method calls. Encapsulation makes it possible, for example, to limit the caller, or to read the contents of a field but not to write it.

Implementation of encapsulation

Encapsulation by setting access control Use access modifiers to set access control The access modifiers are as follows

name Permission range
private Only my own class
protected The same package as you or a class that inherits you
public All classes

Encapsulation formula

Private in the field The method is public

Prepare getters and setters to access the fields

Validate arguments in setter

Purpose of inheritance

Used to reuse similar classes

Benefits of inheritance

By adding methods and fields to the parent element, it is not necessary to set them individually for the child elements. The behavior can be changed individually by overriding.

Implementation example of inheritance


class Car {
  private double gas;

class superCar extends Car {
  private int speed;

This way, the supercar can have a speed field in addition to the Car gas field.


Force the class that defines the method and implements the interface to define the defined method Multiple inheritance is allowed

Implementation example

interface Human {
  public void run();
class taro implements Human { 
  //taro raises an error because no run method is defined

Recommended Posts

About encapsulation and inheritance
About encapsulation
About object-oriented inheritance and about yield Ruby
About class inheritance.
About Ruby inheritance
About java inheritance
Inheritance and interface.
About Bean and DI
About classes and instances
About inheritance (Java Silver)
About redirect and forward
Ruby inheritance and delegation
About Serializable and serialVersionUID
About for statement and if statement
About synchronized and Reentrant Lock
[Java] About String and StringBuilder
About the same and equivalent
About classes and instances (evolution)
About pluck and ids methods
Consideration about classes and instances
About Java Packages and imports
About Ruby and object model
About Ruby classes and instances
About instance variables and attr_ *
About self-introduction and common errors
Java encapsulation and getters and setters
About C # lambda expressions and Linq
About Java static and non-static methods
About fastqc of Biocontainers and Java
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
About Gradle's setup phase and execution phase
[PHP] Inheritance, protected and reference patterns
About go get and go install from Go1.16
About removeAll and retainAll of ArrayList
About =
About if statement and branch processing
About Java primitive types and reference types
Consideration about Rails and Clean Architecture
This and that about Base64 (Java)
About Docker, disguise server and container
[Swift] A note about function and closure
About the operation of next () and nextLine ()
Let's be lazy with inheritance and override
[About JDBC that connects Java and SQL]
About the difference between irb and pry
About the mechanism of the Web and HTTP
Find out about instance methods and self
About "Dependency Injection" and "Inheritance" that are easy to understand when remembered together