three major featuresPackage
The so-called encapsulation, that is, to encapsulate objective things into abstract classes, and classes can be their own data and methods only let the trusted class or object operation, to the untrusted information hiding. encapsulation is one of the object-oriented features and is the main feature of object and class concepts. simply put, a class is a logical entity that encapsulates the data and the code that operates the data. Within an object, some code or some data can be private and cannot be accessed by the outside world. In this way, objects provide different levels of protection to internal data to prevent unintended changes to unrelated parts of the program or to use the private parts of the object incorrectly.
encapsulation literally is the meaning of packaging, the professional point is information hiding, refers to the use of abstract data types to encapsulate data and data-based operations together to form an indivisible independent entity, the data is protected in the abstract data type inside, as far as possible to hide the internal details, Only some external interfaces are retained to make them contact external. Other objects of the system can communicate and interact with this encapsulated object only through authorized operations that are wrapped outside the data. This means that the user does not need to know the details inside the object (and of course it is unknown), but can access the object through the interface provided by the object externally.
For encapsulation, an object encapsulates its own properties and methods, so it does not need to rely on other objects to do its own work.
There are three major benefits to using encapsulation:
1, good package can reduce the coupling.
2, the structure within the class can be freely modified.
3, the member can be more precise control.
4, hide information, realize the details.
inheritance
inheritance refers to the ability to use all the functionality of an existing class and to extend these capabilities without rewriting the original class. a new class created through inheritance is called a subclass or derived class, and the inherited class is called the base class, parent class, or superclass. The process of inheritance is from the general to the special process. To implement inheritance, it can be implemented through inheritance (inheritance) and combination (composition). The concept of inheritance is implemented in two categories: implementation inheritance and interface inheritance. Implementation inheritance refers to the ability to directly use the properties and methods of the base class without additional coding; Interface inheritance is the ability to use only the names of properties and methods, but subclasses must provide the implementation;
Inheritance defines how classes are associated with each other and share attributes. For several of the same or known classes, we can abstract out their common behavior or zodiac and define them as a parent class or superclass, and then inherit the parent class with these classes, and they can not only own the properties of the parent class, methods, but also define their own unique properties or methods.
At the same time, you need to remember three words when using inheritance:
1. Subclasses have properties and methods that are not private to 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 the parent class in their own way.
Polymorphic
the so-called polymorphic means that the same method of a class instance has different manifestations in different situations. Polymorphic mechanisms enable objects with different internal structures to share the same external interface. This means that although specific actions are different for different objects, they can be called in the same way through a common class.
The most common polymorphism is to pass a subclass into the parent class parameter, and the runtime invokes the parent class method to determine the specific internal structure or behavior through the passed-in subclass.
In the beginning, it was mentioned that inheritance was prepared for polymorphic implementations. Subclass Child inherits the parent class father, we can write a reference to the parent class type of the child class, which can handle either the parent class Father object or the subclass child object, when the same message is sent to the child class or the parent class object. The object performs different behavior depending on the reference to which it belongs, which is polymorphic. That is, polymorphism is the same message that makes different classes respond differently.
There are three prerequisites for implementing polymorphism in Java: inheritance, rewriting, and upward transformation.
Inheritance: Subclasses and parent classes that have inheritance relationships must exist in polymorphism.
Rewrite: Subclasses redefine some methods in the parent class and call the methods of the subclasses when they are called.
Upward transformation: In polymorphic, a reference to a subclass needs to be assigned to the parent class object, so that the reference can have the ability to invoke methods and subclasses of the parent class.
Java Fundamentals Summary (i)