Java is a pure object-oriented programming language, it has three major characteristics, namely encapsulation, inheritance and polymorphism.
First, the package
Encapsulation is to encapsulate the properties and methods of an object so that the user knows the properties and methods provided by the object without having to explore its internal implementation. For example, a mobile phone is a packaged object, when you call the phone when you just enter the phone number on it, and not to control how the mobile phone is working. Adopting the principle of encapsulation can make the data outside the object not easily accessible to change the internal data of the object, effectively avoid the influence of external errors on the internal information, and the encapsulation can also improve the maintenance of the program, when a part of the error, as long as the interface has not changed, other places do not have to change.
Ii. inheritance
Inheritance is understood as the usual meaning that the son inherits the property from the father, where inheritance and that inheritance are the same meaning, but inheritance is not a property, but a property and a method, and the entity is not a son and a father, but a subclass and a parent class. For example, there are many animals in the zoo, including birds, tigers and so on. Then we can write an animal class, a bird. All creatures in the zoo are collectively known as animals, which means that animals have properties and methods that are common to all creatures in the zoo. Birds also belong to animals, but each animal has different characteristics, such as birds can fly, which means that birds also have its own unique properties and methods. Therefore, all the animals in the zoo can be regarded as having the same properties and methods, but also have their own unique properties and methods. And those same attributes and methods can be seen as inheriting from the parent class. So, birds are subcategories of animals, animals are the parents of birds, and in declarations of classes. You can use the keyword extends to indicate its parent class.
Syntax format: [modifier] class subclass name extends parent class name {}
Public classanimal{//Parent Class intAge ; String sex;//Animals have properties Public voideat () {System.out.println ("Eat something");//animals have a way } Public voidsleep () {System.out.println (Sleep); }} Public classBirdextendsanimal{//sub-classString type; Public voidFly () {eat ();//calling methods from the parent classsleep (); System.out.println ("Birds can fly."); }}
Subclasses can inherit all properties and methods in the parent class that can be inherited by the quilt, but there are a few things to be aware of.
1. Only properties and methods that are modified by public and protected in the parent class can be inherited, and cannot be inherited by private adornments.
2. If a subclass declares a method and a property with the same name as the parent class, the properties and methods of the parent class are hidden and overwritten and cannot be inherited.
Public classanimal{//Parent Class intAge ; String sex;//Animals have properties Public voideat () {System.out.println ("Eat something");//animals have a way } Public voidsleep () {System.out.println (Sleep); }} Public classBirdextendsanimal{//sub-class String type; String sex;//Animals have properties Public voideat () {System.out.println ("Eat something");//animals have a way} Public voidFly () {sleep (); System.out.println ("Birds can fly."); }}//at this point the parent class's Sex and eat () will be hidden and overwritten, and the subclass calls its own sex and eat () instead of the parent class.
Just said that if you declare the same properties and methods in the child class as the parent class, the property and method of the parent class will be hidden, what if you want to use the methods and properties of the parent class? This is going to use the Super keyword.
There are two main uses of the Super keyword.
1. Call the constructor method of the parent class.
Format: Super (argument list), but this statement must be the first statement of the subclass construction method, otherwise an error.
2. Manipulate the properties and methods that are hidden.
Format: Super. Properties, Super. method.
Public classanimal{intAge ; String sex;//Animals have properties PublicAnimal (intage,string Sex) { This. Age =Age ; This. Sex =sex; } Public voideat () {System.out.println ("Eat something");//animals have a way } Public voidsleep () {System.out.println (Sleep); }} Public classBirdextendsanimal{String type; PublicBird (intage,string sex,string type) { Super(Age,sex);//Super Call the parent constructor method, put in the first sentence This. Type =type; } Public voideat () {System.out.println ("Bird Eats");//animals have a way } Public voidFly () {Super. Eat ();//calling a hidden methodsleep (); System.out.println ("Birds can fly."); } }
Three, polymorphic
Polymorphism is one of the three major features of the Java language, often using method overloading and method overrides to achieve polymorphism.
Method overloading is a method in which multiple parameter lists appear in a class (you can make different number of arguments, or different parameter types, or they can be of different order of parameters) but the method has the same name. When you overload a method, the return value of the method cannot be a flag that distinguishes the method overload.
The method overrides the method that generally refers specifically to subclasses overriding the parent class. For example, the Eat () in the bird class has covered the Eat () in animal, and the overlay embodies the ability of the subclass to complement or modify the parent's method, which allows the same method to behave differently in different subclasses.
Public class extends animal{ publicvoid eat () { System.out.println ("Bird Eats, with Peck"); animals have methods } }publicclassextends animal{ publicvoid eat () { System.out.println ("The dog eats something, with licking"); animals have the method } }
Similarly, there are a few points to note in method overrides:
1. Methods that cannot overwrite the final and static modifiers in the parent class
2. The subclass must overwrite the method in the parent class that is being modified, or declare the method abstract.
3. When a subclass overrides a method of the same name as the parent class, it must be the same as the declaration of the parent class method.
up: A class can be considered a class type, or it can be considered a parent class or a superclass type. Gets a reference to an object and sees it as the object of its parent class, called an upward transformation.
New Bird (); New Dog (); At this point it's all up, but it's not written New Animal ();
java--three main features of object-oriented