Inheritance, polymorphism and encapsulation in java

java is an object-oriented language. Therefore java has all the characteristics of object-oriented languages: inheritance, polymorphism and encapsulation.

        Inheritance: The smallest unit in java is a class; inheritance means that a class can obtain variables and methods that are allowed to be obtained from another class; inheritance is also an important way for java to improve code reuse. So what variables or methods can be obtained in inheritance, and what variables or methods cannot be obtained? This involves the concept of access modifiers. Access modifiers in java are: public, protected, default and private. Among them, public is visible to everyone; protected is visible to this package, this class and subclasses, but not to other packages; default is visible to this package, this class is visible to other packages and subclasses; private is visible to this class, but this package, Subclasses and other packages are not visible. It can be seen that the variables or methods modified by the public and protected access modifiers can be inherited, while the variables or methods modified by the default (default, that is, no access modifier is written) and private access modifiers cannot be inherited. The class that is inherited in inheritance is called the parent class (or superclass), and the class that inherits the parent class is called the subclass. The subclass can obtain all the variables or methods of the parent class that are allowed to be inherited. The requirements of the subclass itself define the variables or methods that belong to the subclass, thus realizing the reusability of the code. It should be noted that the inheritance between classes in java is single inheritance, that is, a subclass cannot inherit multiple classes, and the extends keyword is used for inheritance. Extends inheritance can also be used between interfaces, but compared to single inheritance between classes, the inheritance between interfaces can be multi-inherited, that is, extends interface one [, interface two, interface three...] ;In this syntax, you only need to use extends once, after which you can write multiple interfaces, separated by commas; in addition to inheritance, there is another kind of inheritance in java called implementation, which generally exists in a A class implements (inherits) an interface and uses the implements keyword. A general class implementing an interface must override all methods in the interface, while an abstract class does not need it. An abstract class is defined using the abstract keyword, and abstract methods can be stored in abstract classes. You can also omit abstract methods (define ordinary variables and methods like ordinary classes), abstract classes cannot be instantiated, but all variables and methods in abstract classes can be obtained through inheritance.

        Polymorphism: Polymorphism means that under the premise of the existence of inheritance relationship, when the parent class is instantiated, it can be instantiated with the type of its subclass. Can be paired with method overriding to maximize code reuse.

        Encapsulation: Encapsulation is the operation of encapsulating some data. Since the smallest unit of Java is a class, classes are generally used to encapsulate some data (such as variables and methods), and access modifiers are used to control them, which can be controlled by getter and setter methods. Operate on the encapsulated data.


    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324667978&siteId=291194637