My simple understanding of the three main Java object-oriented features

Java has three object-oriented features: encapsulation, inheritance, and polymorphism.

Encapsulation: Encapsulation is the combination of data and behavior into a class, which is an object and protects the encapsulated data, i.e., privatizes it, and then provides access to the data (getter and setter methods), which can only be accessed by getter and setter methods, through which we can enter the data.Row protection, for example, for some data that can only be read and cannot be modified, we can only provide getter methods.Applying encapsulation can improve code reliability.The most common encapsulation, the po class:

public class Encapsulation {
	private String variable;   //Privatization

	public String getVariable() {   //Provides a getter method for getting attribute data
		return variable;
	}
	public void setVariable(String variable) {  //Provides setter methods for modifying properties
		this.variable = variable;
	}
}

1.1 this keyword: this keyword is often used when encapsulating objects.

this refers to:

In a common method: this points to the object calling the method

In the construction method: this points to the object being modified

1.2 Internal Classes:

Internal classes can directly access the properties and methods of external classes, but external classes cannot directly access the properties of internal classes.

Internal classes can only be accessed through external classes, which improves security.

1.2.1 Member internal class:

Internal classes directly access the properties and methods of external classes:

public class OutClass {
	
	private String a = "Properties of External Classes";
	
	public void outRun() {
		System.out.println("External Class Method");
	}

         //Member Inner Class
	public class InClass{
		public void inRun() {
			System.out.println(a);    //Direct access to external class properties
			outRun();   //Direct access to external class methods
		}
	}
	
	@Test
	public void run() {
		new OutClass().new InClass().inRun();   //Use internal class methods
	}
}

Run result:

Properties of External Classes
 External Class Method

External classes access properties and methods of internal classes:

public class OutClass {
	
	public void outRun() {
		InClass inClass = new InClass();
		System.out.println(inClass.b);   //Accessing internal class properties through internal class objects
		inClass.inRun();    //Accessing internal class methods through internal class objects
	}

	//Member Inner Class
	public class InClass{
		private String b = "Internal Class Properties";
		public void inRun() {
			System.out.println("Internal Class Method");
		}
	}
	
	@Test
	public void run() {
		new OutClass().outRun();   //Using external class methods
	}
}

Run result:

Internal Class Properties
 Internal Class Method

Both tests show that internal classes have direct access to the properties and methods of external classes, while external classes can only access the properties and methods of internal classes through internal class objects.

1.2.2 Static internal class:

Because static internal classes are static, they cannot directly access the non-static properties and methods of external classes and can be accessed through external class objects.

When creating static internal class objects, external class objects are not required.

public class OutClass {
	
	private String outA = "External Class Non-Static Properties";
	private static String outB = "External Class Static Properties";
	
	public void outRun() {
		System.out.println("External Class Nonstatic Method");
	}
	public static void outStaticRun() {
		System.out.println("External Class Static Method");
	}

	//Static Internal Class
	public static class InClass{
		public void inRun() {
			System.out.println(outB);  //Direct access to static properties of external classes
			outStaticRun();    //Static methods that directly access external classes
			
			System.out.println(new OutClass().outA);  //Accessing External Class Properties through External Class Objects
			new OutClass().outRun();    //Accessing external class methods through external class objects
		}
	}

	
	@Test
	public void run() {
		new InClass().inRun();  //Using static internal class methods, you can create internal class objects directly without requiring external class objects
	}
}

Run result:

External Class Static Properties
 External Class Static Method
 External Class Non-Static Properties
 External Class Nonstatic Method

You can see that static internal classes have direct access to static members of external classes, while to access non-static members, you need to create external class objects to invoke.

1.2.3 Method Internal Class:

A method internal class is a class defined inside the method of an external class that can only be used in this method.

Method internal classes cannot use access modifiers and static modifiers.

public class OutClass {
	//External Class Method
	public void outRun() {
		//Method Internal Class
		class InClass{    //Note: public, protected, default, private access modifiers cannot be used here 
                                                                   // And static static modifiers
			private String a = "Attributes of class inside method";
			public void inRun() {
				System.out.println("Method of method internal class");
			}
		}
		
		InClass inClass = new InClass();  //Create method internal class object
		System.out.println(inClass.a);    //Use method internal class properties by object
		inClass.inRun();   //Using method internal class methods with objects
	}

	@Test
	public void run() {
		//Call External Class Method
		new OutClass().outRun();
	}
}

Run result:

Attributes of class inside method
 Method of method internal class

2. Inheritance:

Inheritance can be said to be an abstraction of methods, abstracting the public attributes of a batch of methods so that code can be more reusable.

Inheritance can only be single inheritance, and subclasses inherit the parent class, which gives them all the properties and methods of the parent class, but private properties are not directly accessible.

         

//Parent Class
public class Father {
	private String privateStr = "Private property of parent class";
	public String publicStr = "Public attributes of the parent class";

	public String getPrivateStr() {
		return privateStr;
	}
	public void setPrivateStr(String privateStr) {
		this.privateStr = privateStr;
	}

	void fatherRun() {
		System.out.println("Method of parent class");
	}
}

//Subclass
public class Child extends Father {       }

//Test Class
public class MyTest {
	@Test
	public void run() {
		Child child = new Child();
		
		System.out.println(child.publicStr);   //Direct access to public attributes of parent classes
		System.out.println(child.getPrivateStr());  //Private properties of a parent class can only be accessed through getter s provided by the parent class    
                                                                          // And setter access
		child.fatherRun();   //Accessing parent methods
	}
}

Run result:

Public attributes of the parent class
 Private property of parent class
 Method of parent class

2.1 Method override:

Subclasses can override parent methods, preferring subclass overrides when called.

//Parent Class
public class Father {
	void run1() {
		System.out.println("Method of parent class run1");
	}
	void run2() {
		System.out.println("Method of parent class run2");
	}
}
//Subclass
public class Child extends Father {
	@Override
	void run1() {
		System.out.println("Override methods for subclasses");
	}
}
//Test Class
public class MyTest {
	@Test
	public void test() {
		new Child().run1();   //Override method for accessing subclasses first
		new Child().run2();
	}
}

Run result:

Override methods for subclasses
 Method run2 of parent class

As you can see here, when a subclass overrides its classification method, it gives priority to accessing the override method of the subclass.

3. Polymorphism: The same reference calls the same method with different results.

Conditions for polymorphism to occur: 1, Inheritance 2, Rewrite Method 3, Up-transition upCasting

//Parent Class
public class People {
	void run() {
		System.out.println("I am a person");
	}
}
//Subclass
public class Student extends People {
	@Override
	void run() {
		System.out.println("I am a student");
	}
}
//Subclass
public class Teacher extends People{
	@Override
	void run() {
		System.out.println("I am a teacher");
	}
}
//Test Class
public class MyTest {
	@Test
	public void test() {
		People people = null;
				
		people = new Teacher();    //Up-transition
		people.run();
		
		people = new Student();    //Up-transition
		people.run();
	}
}

Run result:

I am a teacher
 I am a student

 

Keywords: Java Attribute

Added by Thoaren on Thu, 16 May 2019 08:57:33 +0300