Home >Java >javaTutorial >How to implement object-oriented programming in java

How to implement object-oriented programming in java

王林
王林forward
2023-04-29 19:10:051439browse

1: It is allowed to pay references of subclasses to objects of the parent class, but those members in the subclass that are not inherited from the parent class will no longer be visible.

Example:

Bus bus=new Bus();  Car car=bus;

But the car instance cannot refer to the non-inherited fields in the bus, such as the number of passengers in p.

2: this and super

this refers to the variables or methods of the current class, and super refers to the variables or methods of the direct parent class of the current object

3: Polymorphism

The same name can have multiple implementation states. Within the same class, you can have methods with the same name and different parameter lists. Subclasses can also have the same parent class. Variables and methods with the same name.

1) Override, when a subclass inherits the parameterless function of the parent class with the same name, the newly written method of the subclass overrides the method of the parent class (the return type must be the same)

2) Overload inherits the parameterized function of the same name from the parent class, and the method parameters are called overloaded when the parameters are different, so that the method has its own characteristics.

3) Overwrite the method with the same name of the current class. There can be multiple methods with the same name in the same class. Which one to use is determined by the different number and type of parameters passed to them. method.

Overwriting and overloading are for parent classes, and rewriting is for subclasses.

4: Abstract class definition

An abstract class can have its own data members, non-abstract member methods, or abstract methods. Abstract methods only There is no function body in the function definition part.

When a subclass inherits an abstract class, it must implement all abstract methods.

5: Interface

Java only supports single inheritance. In order to achieve multiple inheritance, an interface is designed. A class can only have one direct parent class, but can implement multiple interfaces.

Methods in interfaces can have parameter lists and return types, but do not have any method bodies.

Interfaces can contain fields, but they will be declared static and final

Fields in an interface can only be stored in the static storage area of ​​the interface and do not belong to the interface. .

The methods in the interface are public

To extend an interface to generate a new interface use the keyword extends, to implement an interface use implements.

The method of the java interface can only be Abstract and public, Java interfaces are more abstract than Java abstract classes.

6: The difference between interfaces and abstract classes

1) Abstract classes can provide implementation methods, but interfaces cannot. If you add a new method to an abstract class, all its subclasses will get this method at once, but for the interface, all classes that implement this interface must implement this method.

2) An abstract class can only inherit one, but can implement multiple interfaces.

"Default adaptation mode": The java interface is at the top, followed by the abstract class.

3) Interfaces can be nested, either by a class or by an interface.

7: Static class

is a class that defines static methods, static variables, and static code blocks. Static classes cannot be instantiated, and all members are static. This means telling the compiler that this method can be used without creating an object of this type. Static classes generally need to be instantiated before they can be operated. Static classes are loaded into memory at runtime, so they do not need to be initialized and have no instances, so this cannot be used inside the class.

The above is the detailed content of How to implement object-oriented programming in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete