Java object oriented -- three characteristics

Java object oriented -- three characteristics

Three characteristics of object oriented

1, Encapsulation
In a broad sense, packaging is to keep and seal up items
The object-oriented encapsulation refers to:
Privatize the properties of the class (no longer provide a way for the outside world to directly access the properties),
Provide corresponding getter and setter methods to access and set properties.
Purpose of encapsulation: data security and privacy

Advantages of packaging

  1. Good packaging can reduce coupling.

  2. The structure inside the class can be modified freely.

  3. You can have more precise control over member variables.

  4. Hide information and realize details.

Code example:

public class Animal {
	private String name;
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

public void setAge(int age){
	this.age = age;
}
public class Test {
	private String name;
	private String idNum;
	private int age;

	public int getAge() {
		return age;
	}

	public string getName() {
		return nane;
	}

	public String getIdNum() {
		return idNum;
	}

	public void setAge(int newAge) {
		age = newAge;
	}

public void setName(string newName)i
name = newName;
}

	public void setIdNum(string newId) {
		idNum = newId;
}

2, Inherit

Concept:

  1. Inheritance is a cornerstone of java object-oriented programming technology because it allows the creation of hierarchical classes.
  2. Inheritance is that a subclass inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the instance domain and method of the parent class, or the subclass inherits the method from the parent class, so that the subclass has the same behavior as the parent class.

Format:

class Parent class {
}
 
class Subclass extends Parent class {
}

Inherited properties

  1. Subclasses have non private properties and methods of the parent class.

  2. Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.

  3. Subclasses can implement the methods of their parent classes in their own way.

  4. Java inheritance is single inheritance, but multiple inheritance is allowed. Single inheritance means that a subclass can only inherit one parent class. Multiple inheritance means, for example, class B inherits class A and class C inherits class B. therefore, according to the relationship, class B is the parent of class C and class A is the parent of class B. This is a feature of Java inheritance different from C + + inheritance.

  5. It improves the coupling between classes (the disadvantage of inheritance, the higher the coupling will cause the closer the connection between codes, the worse the code independence).

Inherit Keywords:

Inheritance can be realized by using the keywords extends and implements, and all classes inherit from java.lang.Object. When a class does not inherit the two keywords, it inherits the object (this class is in the java.lang package, so it does not need to import) ancestor class by default.

  1. extends
    In Java, class inheritance is a single inheritance, that is, a subclass can only have one parent class, so extensions can only inherit one class.
public class Animal{
	private String name;
	private int age;
	public void eat() {  
	//method
	} 
    public void sleep() { 
	//method
 	} 
}

public class Penguin  extends  Animal{ 
}
  1. implements keyword
    Using the implements keyword can make java have the feature of multi inheritance in a disguised form. If the scope of use is class inheritance interface, you can inherit multiple interfaces at the same time.
public interface A {
    public void eat();
    public void sleep();
}
 
public interface B {
    public void show();
}
 
public class C implements A,B {
}

3, Polymorphism
Polymorphism is the ability of the same behavior to have multiple different forms or forms.

Advantages of polymorphism

  1. Eliminate coupling between types
  2. Replaceability
  3. Extensibility
  4. Interface
  5. flexibility
  6. Simplification

Implementation of polymorphism
Method 1: Rewrite:
This content has been discussed in detail in the previous chapter, so it will not be described. It can be accessed in detail: Java override and overload.
Mode 2: Interface

  1. The most representative interface in life is the socket. For example, a three connector plug can be connected to a three hole socket, because each country has its own interface rules. It may not be possible to go abroad, because of the interface types defined by foreign countries.

  2. The interface in Java is similar to the interface in life. It is a collection of method features, but there is no method implementation. For details, see the chapter java interface.

class Shape {
    void draw() {}
}
 
class Circle extends Shape {
    void draw() {
        System.out.println("Circle.draw()");
    }
}
 
class Square extends Shape {
    void draw() {
        System.out.println("Square.draw()");
    }
}
 
class Triangle extends Shape {
    void draw() {
        System.out.println("Triangle.draw()");
    }
}

When calling a method in a polymorphic way, first check whether the method exists in the parent class. If not, the compilation error will occur; If so, call the method with the same name of the subclass.

The benefits of polymorphism: it can make the program well extended, and can deal with all class objects in general.

public class Test {
    public static void main(String[] args) {
      show(new Cat());  // Call the show method as a Cat object
      show(new Dog());  // Call the show method with a Dog object
                
      Animal a = new Cat();  // Upward transformation  
      a.eat();               // Cat eat is called
      Cat c = (Cat)a;        // Downward transformation  
      c.work();        // Cat work is called
  }  
            
    public static void show(Animal a)  {
      a.eat();  
        // Type judgment
        if (a instanceof Cat)  {  // What cats do 
            Cat c = (Cat)a;  
            c.work();  
        } else if (a instanceof Dog) { // What dogs do 
            Dog c = (Dog)a;  
            c.work();  
        }  
    }  
}
 
abstract class Animal {  
    abstract void eat();  
}  
  
class Cat extends Animal {  
    public void eat() {  
        System.out.println("Eat fish");  
    }  
    public void work() {  
        System.out.println("Catch a mouse");  
    }  
}  
  
class Dog extends Animal {  
    public void eat() {  
        System.out.println("Eat bones");  
    }  
    public void work() {  
        System.out.println("Housekeeping");  
    }  
}

super and this keywords
Super keyword: we can access the members of the parent class through the super keyword, which is used to reference the parent class of the current object.
this keyword: refers to your own reference.

class Animal {
  void eat() {
    System.out.println("animal : eat");
  }
}
 
class Dog extends Animal {
  void eat() {
    System.out.println("dog : eat");
  }
  void eatTest() {
    this.eat();   // this calls its own method
    super.eat();  // super calls the parent method
  }
}
 
public class Test {
  public static void main(String[] args) {
    Animal a = new Animal();
    a.eat();
    Dog d = new Dog();
    d.eatTest();
  }
}

rewrite
We will show how the behavior of overridden methods affects polymorphism when designing classes in Java.
We have discussed method rewriting, that is, the subclass can override the method of the parent class.
When a subclass object calls an overridden method, it calls the subclass's method, not the overridden method in the parent class.
To call the overridden method in the parent class, you must use the keyword super.

public class Anmail {
	public void eat{
		//Parent code block
	}
}

public class Dog extends Anmail {
	public void eat{
		//Write a code block that is different from the parent class
	}
}

Tags: Java encapsulation Polymorphism

Posted by sdaniels on Mon, 26 Jul 2021 03:05:19 +0930