Home Java javaTutorial Understanding the three major features of Java: encapsulation, inheritance, and polymorphism

Understanding the three major features of Java: encapsulation, inheritance, and polymorphism

Jan 19, 2017 pm 02:08 PM

First of all, let’s briefly talk about the definition of its three major features:

Encapsulation: hiding the properties and implementation details of the object, only exposing the interface to the outside world, and controlling the reading and modification of properties in the program. access level. Combining the abstracted data and behaviors (or functions) to form an organic whole, that is, organically combining the data with the source code that operates the data to form a "class", in which data and functions are both members of the class. The purpose of encapsulation is to enhance security and simplify programming. Users do not need to understand the specific implementation details, but only need to use the members of the class through the external interface and a specific access permission. The basic requirements for encapsulation are: privatize all properties and provide getter and setter methods for each property. If there is a constructor with parameters, you must write a constructor without parameters. During development, it is often necessary to test the classes that have been written, so sometimes the toString method is overridden, but this is not necessary.

Inheritance: Code reuse is achieved through inheritance. All classes in Java are obtained by directly or indirectly inheriting the java.lang.Object class. The inherited class is called a subclass, and the inherited class is called a parent class. Subclasses cannot inherit member variables and methods with private access rights in the parent class. Subclasses can override methods of the parent class and name member variables with the same name as the parent class. But Java does not support multiple inheritance, which is the ability of a class to derive from multiple superclasses. Try to reduce inheritance relationships as much as possible during development to reduce the coupling of the program.

Polymorphism: Polymorphism is divided into design-time polymorphism and run-time polymorphism. For example, overloading is also called design-time polymorphism, and for overridden or inherited methods, JAVA runtime The system determines which method to call based on the type of instance on which the method is called, which is called runtime polymorphism. All in all, the typical characteristics of object-oriented design are inheritance, encapsulation and polymorphism. These characteristics are also the key to why object-oriented is so popular.

Encapsulation

The default value of access permissions for class attributes in Java is not private. If you want to hide the method of this attribute, you can add the private modifier to restrict it to only within the class. access.

For the private attributes in the class, a pair of methods (getXXX, setXXX()) should be provided to access the private attributes to ensure the operation and security of the private attributes.

Method encapsulation, what should be made public, what should be hidden should be hidden.

Inheritance of java

Inheritance is the abstraction of multiple types of things with common characteristics into one class.

Inheritance in java must use the extends keyword, and java allows single inheritance, that is, a class can only have one parent class.

Constructor methods cannot be inherited.

Override in java method

When a subclass has a method with the same name and the same parameter list that is accessible in the parent class, the method inherited from the parent class will be overwritten.

super() keyword

super() means that when the constructor of the subclass calls the constructor of the parent class, super() can only be used in the constructor. first sentence.

Polymorphism in java

There are two polymorphic mechanisms: compile-time polymorphism and run-time polymorphism

1. Overloading of methods: Overloading is It means that there are multiple methods with the same name in the same class, but these methods have different parameters. , so you can determine which method to call at compile time. It is a kind of compile-time polymorphism.

2. Method coverage: Subclasses can override the methods of the parent class, so the same method will have different expressions in the parent class and in the subclass. In the Java language, a reference variable of a base class can point not only to an instance object of the base class, but also to an instance object of a subclass. Similarly, a reference variable in an interface can also point to an instance object of its implementation class.

public class A {
public String show(D obj) {
return ("A and D");
}
public String show(A obj) {
return ("A and A");
} 
}
public class B extends A{
public String show(B obj){
return ("B and B");
}
public String show(A obj){
return ("B and A");
} 
}
public class C extends B{
}
public class D extends B{
}
public class Test {
public static void main(String[] args) {
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D(); 
System.out.println("1--" + a1.show(b));
System.out.println("2--" + a1.show(c));
System.out.println("3--" + a1.show(d));
System.out.println("4--" + a2.show(b));
System.out.println("5--" + a2.show(c));
System.out.println("6--" + a2.show(d));
System.out.println("7--" + b.show(b));
System.out.println("8--" + b.show(c));
System.out.println("9--" + b.show(d)); 
}
}
1--A and A
2--A and A
3--A and D
4--B and A
5--B and A
6--A and D
7--B and B
8--B and B
9--A and D
Copy after login

When a superclass object reference variable refers to a subclass object, the type of the referenced object rather than the type of the reference variable determines whose member method is called, but the called method must be in the superclass Defined, that is to say, methods overridden by subclasses.

Let’s use an example to illustrate the meaning of this sentence: a2.show(b);

Here a2 is a reference variable, which is of type A, and it refers to It is a B object, so according to the above sentence, it means that B decides whose method to call, so a2.show(b) should call show(B obj) in B, and the result should be "B and B", but why is it different from the previous running results? Here we ignore the following sentence "But the method called here must be defined in the super class", then does show(B obj) exist in class A? It doesn't exist! So this sentence doesn't apply here? So is this sentence wrong? No! In fact, this sentence also implies this sentence: it still needs to be confirmed according to the priority of the calling method in the inheritance chain. That's why it will find show(A obj) in class A. At the same time, because B has rewritten this method, it will call the method in class B. Otherwise, it will call the method in class A.

The above is the understanding of the three major characteristics of Java encapsulation, inheritance, and polymorphism introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will promptly Reply to everyone. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to understanding the three major characteristics of Java encapsulation, inheritance, and polymorphism, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

See all articles