Three features of Java oop (encapsulation, inheritance, polymorphism)

encapsulation

As the name suggests, it is to encapsulate the data to improve the security of the data. Our program is to pursue "high cohesion, low coupling". High cohesion means that the internal data operation details of the class are completed by themselves, without external interference and low coupling; Only a small number of methods are exposed for external use.

The encapsulated keyword is private. For example:

public class Tao{
    private int age;
}
public static void main(String[] args){
    Tao a=new Tao();
    a.age//Here is a direct error report
}

a.name reports an error because the attribute has been privatized and cannot be directly used externally. If you want to manipulate the privatized property, you can create a set and get method. It is quite possible to provide an interface to the outside.

public class Tao{
    private int age;
    public void setAge(int age){
        this.age=age;
    }
    public int getAge(){
        return this.age;
    }
}
public static void main(String[] args){
    Tao a=new Tao();
   a.setAge(18);
}

As can be seen from the above, we can assign values through a set method, and the get method is the value.

In order to verify the validity of the data, we can perform some data verification in the set method

public class Tao{
    private int age;
    public void setAge(int age){
        if(age>0&&age<110){
          this.age=age;  
        }else{
           this.age=18; 
        }
        
    }
    public int getAge(){
        return this.age;
    }
}

As can be seen above, I verified the age. The incoming age value can only be less than 110 years old and must be greater than 0 years old. Otherwise, I gave a default value of 18. You can't just operate without packaging.

Then summarize the benefits of a package:

  • Improve program security and protect data.
  • Implementation details of hidden code.
  • Unified interface.
  • System maintainability increased.

inherit

In fact, the essence of extensions abstracts a class. Inheritance is a relationship between classes. In addition, there are dependency, combination, aggregation and other relationships between classes. There are two classes of inheritance relationship, one is a child class (derived class) and the other is a parent class (base class). The child class inherits the parent class, which is represented by the keyword extensions. In a sense, the relationship between subclass and parent has the relationship of is a. In java, there is no multi inheritance, but single inheritance. Subclasses can inherit all properties and methods of the parent class. However, if the parent property is set to private, the child class cannot inherit.

  1. object class

    In Java, all classes inherit the object class, but Java hides it, just like a parameterless constructor.

  2. super

    The super keyword is the same as the this keyword, but this refers to the current class and super refers to the parent class. It can call the methods and properties of the parent class. This and super cannot call construction methods at the same time. Super can only appear in subclass methods or construction methods.

  3. rewrite

    You need to have an inheritance relationship. The subclass overrides the method of the parent class.

    • Method names must be the same
    • The parameter list must also be the same
    • The modifier range can be expanded, but not reduced
    • The range of exceptions thrown can be reduced, but not enlarged.
    • The method body can be different
    • Static, final and private modified methods cannot be overridden.

‚Äč Why rewrite? The basic reason is that the of the parent class can't meet the needs of the child class.

In order to popularize overloads, overloads need to:

  • Same method name
  • Parameter type, number and order are different
  • The return types can be the same or different.

polymorphic

That is, the same method can take a variety of different behavior modes according to different sending objects. The actual type of an object is determined, but there are many reference types that can point to the object.

Conditions for polymorphism:

  • There is an inheritance relationship
  • Subclass overrides parent method
  • A parent class reference points to a child class object

Note: polymorphism is the polymorphism of methods, and there is no polymorphism of attributes.

Case:

//Animal (parent)
public class animal {
    public  void  eat(){
        System.out.println("Parent class");
    }
}
//Dogs (subclasses)
public class dog extends animal{
    @Override
    public void eat() {
        System.out.println("I'm eating bones");
    }
}
//Cats (subclasses)
public class cat extends  animal{
    @Override
    public void eat() {
        System.out.println("I'm eating fish");
    }
}

//Test class
public class text {
    public static void main(String[] args) {
        animal c=new dog();
        animal d=new cat();
        c.eat();
        d.eat();
    }
}
The output is:
I'm eating bones
 I'm eating fish

The above refers to polymorphism, and the reference of the parent class points to the child class. This shows whether polymorphism can make our program more flexible.

Tags: Java

Posted on Tue, 09 Nov 2021 21:34:22 -0500 by www.WeAnswer.IT