Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of Java Common knowledge points (③)-- object-oriented Foundation

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Object-oriented three elements: encapsulation, inheritance and polymorphism

1. Encapsulation: the meaning of encapsulation is to clearly identify all member functions and data items, or interfaces, that are allowed to be used externally.

two。 Inheritance:

Inherit the methods of the base class and make your own extensions; declare that a subclass is compatible with a base class (that is, the interface is fully compatible with the base class) so that external callers do not have to pay attention to the differences (the internal mechanism automatically dispatches the request dispatch to the appropriate logic).

3. Polymorphism: based on the class to which the object belongs, the external calls to the same method actually execute different logic. It is obvious that polymorphism is actually attached to the second meaning of inheritance.

one。 Polymorphisms

Method signature: method name + parameter list (parameter type, number, order)

1.1 rewrite

The subclass overrides the parent method, only the instance method can be overridden, and the overridden method must still be the instance method. Neither member variables nor static methods can be overridden and can only be hidden.

Rewrite instance method: superclass Parent has instance method A, subclass child defines instance method B with the same signature and subset return type as A, and subclass object ChildObj can only call its own instance method B.

Method rewriting (override) two identical, two small, one major principles:

Method name is the same, parameter type is the same, subclass return type is less than or equal to parent class method return type subclass throws exception less than or equal to parent class method throws exception subclass access is greater than or equal to parent method access

Note:

Static static methods cannot be overridden. (it can be written formally, but it is not rewritten in nature, and belongs to the hidden below.) the rewriting method can change its method modifiers, such as final,synchronized,native. Regardless of whether there is an final-modified parameter in the overridden method, the overridden method can add, retain, and remove the final modifier for this parameter (the parameter modifier is not part of the method signature).

1.2 heavy load

In the same class, there are multiple methods with the same name and different parameter lists (different number of parameters and different parameter types), regardless of the return value of the method and the permission modifier. The compiler can statically compile different methods by identifying the method signature. This is also one of the differences between overloading and rewriting in java.

Overloading is only a language feature, independent of polymorphism and object-oriented. Polymorphism is to achieve interface reuse.

The method in Java can have the same name as the class name, and the only difference from the constructor is that the constructor does not return a value.

1.3 Hidden

Hiding and overriding are very similar in form (grammatical rules), but there is an essential difference: only member variables (static or not) and static methods can be hidden.

1.4 member variables

The superclass Parent has the member variable A, the subclass Child defines the member variable B with the same name as A, and the subclass object ChildObj calls its own member variable B. If you convert a subclass object ChildObj to a superclass object ParentObj, ParentObj calls the superclass member variable A!

When hiding member variables, as long as they have the same name, you can change the variable type (whether primitive or hidden). You cannot hide private member variables in the superclass, in other words, you can only hide accessible member variables. When you hide the superclass member variable A, you can lower or increase access to the subclass member variable B, as long as An is not private. Hiding member variables has nothing to do with static! Static variables can hide instance variables, and instance variables can also hide static variables. You can hide final member variables in a superclass.

1.5 static method

The superclass Parent has static method A, the subclass Child defines static method B with the same signature and subset return type as A, and the subclass object ChildObj calls its own static method B. If you convert a subclass object ChildObj to a superclass object ParentObj, ParentObj calls the superclass's static method A!

The hidden method must still be static

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report