Hello everyone, I'm KookNut39 and Tommy. I was Tommy when writing articles and Java at CSDN. I share some things I think are more important in the learning process. I'm committed to helping beginners get started. I hope it can help you make progress. Interested bloggers are welcome to follow and learn java knowledge with bloggers. You can also check the previous articles in the column. I hope you can discuss technology with you in the future.
Object oriented feature polymorphism
Before learning polymorphism, we should first learn abstract classes and interfaces, because abstract classes and interfaces are the premise of realizing polymorphism.
1. Abstract class
If you move up in the inheritance hierarchy of a class from top to bottom, the class at the upper level is more general and may be more abstract. From a certain point of view, the ancestor class is more general. People only use it as the base class to derive other classes, rather than constructing the specific instance you want.
——Java core technology (Volume I)
The above paragraph explains how abstract classes come from and the purpose of abstract classes. So what is an abstract class? If a class does not contain enough information to describe a specific object, such a class is an abstract class.
In addition to not instantiating objects, abstract classes still have other functions, such as member variables, member methods and construction methods.
A class decorated with abstract is an abstract class. If a class contains abstract methods, the class must be defined as an abstract class.
characteristic:
Abstract classes cannot be instantiated, but they can have constructors. Because abstract classes contain methods without concrete implementation, objects cannot be created with abstract classes.
Abstract classes can only be used as base classes, representing an inheritance relationship. A non abstract class that inherits an abstract class must implement all the abstract methods in it. The parameters and return values of the implemented methods must be the same as those in the abstract class. Otherwise, the class must also be declared abstract.
Abstract method
Abstract methods act as placeholder methods, which are implemented in subclasses.
-
Abstract method is a special method: it has only declaration, but no concrete implementation
-
Abstract methods must be decorated with the abstract keyword
[Access rights] abstract class Class name { //Member list }
be careful:
- Abstract classes and abstract methods are design level concepts in the process of software development. In other words, designers will design abstract classes and abstract methods. Programmers inherit these abstract classes and cover abstract methods to realize specific functions.
- Some programmers believe that concrete methods cannot be included in abstract classes. It is recommended to put common properties and methods (whether abstract or not) in the base class (whether abstract or not).
- You can declare a class as an abstract class even without abstract methods.
- Abstract classes cannot be instantiated.
2. Interface
What we see most in life are USB interface, computer interface, mouse interface and USB flash disk interface. So how do these interfaces work?
The USB interface on the computer itself does not realize any function, which means that when we do not insert anything into it, it is useless, and whether it does or not will have any impact on the computer, but it stipulates the data transmission requirements, which can be realized by many devices. When the mouse is connected to the computer, it will not use the mouse as a USB flash disk, The reason is that what functions are implemented is not determined by the interface, but by who implements it.
Interface is used to describe what a class should do without specifying how it should do it. A class can implement one or more interfaces.
Note: an interface is not a class, but a set of requirements for a class that you want to meet this interface.
Definition of interface:
//The contents in brackets indicate whether there can be or not [Access modifier ] interface Interface name [extends Other interface names 1,....Other interface names n] { //The interface class must be decorated with the interface keyword //The attributes in the interface are public static final by default int num = 10; //All methods are public abstract by default void method(); }
Characteristics of interface:
- All variables in the interface are static constants
- Interfaces are implicitly abstract. When declaring an interface, you do not need to use the abstract keyword.
- Each method in the interface is also implicitly abstract. It defaults to public abstract.
- The attributes declared in the interface are public static final by default;
- Interfaces are not inherited by classes, but implemented by classes.
- An interface cannot instantiate an object (no constructor), but can declare a reference to the object. (polymorphism)
- Multiple classes can implement the same interface.
- A class can implement multiple interfaces, but can only inherit one class.
- Similar to inheritance, there is polymorphism between interface and implementation class.
- One interface can inherit multiple other interfaces.
- When a class implements an interface, the class implements all the methods in the interface. Otherwise, the class must be declared abstract.
During interface oriented programming:
-
Care about the capabilities of the implementation class, not the implementation details
-
Interface oriented conventions without considering the specific implementation of the interface
The meaning of interface: in Java, a class can only have one parent class, so the interface can realize the logic of multiple inheritance.
In essence, an interface is a special abstract class. This abstract class only contains the definitions of constants and methods, without the implementation of variables and methods.
Use of interfaces:
Use of interface: the class uses the implements keyword to implement the interface. In the class declaration, the implements keyword is placed after the class declaration.
[Access modifier ] class Class name implements Interface name 1, interface name 2{ }
Combined inheritance:
[Access modifier ] class Class name extends Parent class name implements Interface name 1, interface name 2{ }
polymorphic
The last blog mentioned that polymorphism is the same thing, showing different states at different times
There are three necessary conditions for polymorphism:
-
Inheritance (or interface implementation) (prerequisite)
-
Rewriting (prerequisite)
-
A parent class reference points to a child class object
Calling member methods in polymorphic environment
class Animal{ void show() { System.out.println("Anmial"); } } class Cat extends Animal{ void show() { System.out.println("cat"); } } Animal x = new Cat(); x.show(); //The method in the subclass is called
There is a picture and a truth:
Just look at the left when compiling and the right when running.
Calling static member methods in polymorphic environment
class Animal{ static void show() { System.out.println("Animal"); } } class Cat extends Animal { static void show() { System.out.println("Cat"); } } Animal x = new Cat(); x.show(); //The static member method in the animal class is called.
That is to say: both compilation and operation are on the left
Calling member variables in polymorphic environment
class Animal{ int num = 3; } class Cat extends Animal { int num = 4; } Animal x = new Cat() ; System.out.println(x.num); //Called is a member variable in the animal class.
That is to say: both compilation and operation are on the left.
Note: variables are not overridden by subclasses. Only methods are overridden.
Method parameters are polymorphic
class Animal{ void eat() {} } class Cat extends Animal{ void eat() {} } class Dog extends Animal{ void eat(){} } //The formal parameter type of the method is the parent type, and the actual parameter passed can be an object of any subclass void method(Animal animal){ animal .eat(); }
The benefits of method parameter polymorphism: improving code scalability
Upward transformation
class Animal{ void eat() { System.out.println("eat"); } } class Cat extends Animal{ void look() { System.out.println("Look at the mouse"); } } Animal x=new Cat(); //Shape up, and the Cat object is promoted to an Animal object x.eat(); //Only methods in the parent class can be used x.look(); //report errors! Methods in subclasses cannot be used
Downward transformation
class Animal{ void eat(){ System.out.println("eat"); } } class Cat extendsAnimal{ void look() { System.out.println("Look at the mouse"); } } Animal x=new Cat(); Cat m=(Cat)x; //Downward transformation m.eat() ; m.look();//Methods in child and parent classes can be used
final keyword
final is used to declare properties, methods, and classes
-
Attribute: the definition must be assigned directly or in the construction method, and cannot be modified later.
-
Method: cannot be overridden in subclass. (cannot be overridden)
-
Method parameters: modify the parameters finally, and add the final keyword in front of the method parameters to prevent data from being modified in the method body.
-
Class: cannot be defined as an abstract class or interface, and cannot be inherited. (cannot be a parent class)
final attribute assignment
-
Assign values at the same time when declaring, which is often used with static
-
When there is no assignment during declaration, it must be assigned one by one in the constructor
-
General principle: ensure that the value of the final attribute is determined when creating each object
This sharing is over here. The codeword is not easy. If you like it, enjoy it + comment + collection 🤞🤞🤞, Thank you for your support
There may be some improper places in the text, such as examples, words, etc. Please understand and welcome criticism and correction in the comment area!
Reference book: Java core technology Volume 1 (version 11)