Java09. Object-oriented. Polymorphism, abstract class, interface

Source: Internet
Author: User

Polymorphism: The multi-medium form of the existence of things

Prerequisite: 1, to have the inheritance relationship; 2, to have a method to rewrite; 3. To have a parent class reference to a child class object

Father f = new Son ();

Member variables: Compile look left, run look left

Member method: Compile look to the left, run to the right (the parent class has the row, if the subclass does not, executes the parent class)

static method: Compile look left, run look left.

It is best to use polymorphism when passed as a parameter (Strong extensibility!) )

Abstract class: A class that is modified by an abstraction (in general, the parent class is defined as an abstract class in development)
Features: A: If a class has an abstract method, the class must be an abstract class (abstract class edges can have no abstract method). B: Abstract classes are not able to be instantiated. Cannot create an object. (If there is no abstract method in the class, the purpose of defining the abstract class is to prevent other classes from establishing this type of object) C: If a class inherits an abstract class, it either overrides all abstract methods in the abstract class, or is itself an abstract class.
Member Features:
Member variables: Subclasses can directly inherit member variables from abstract classes (the member variables in the abstract class can be the same as before).
Member methods: Abstract classes are divided into two methods, one is an abstract method, which must be implemented (overridden) in a subclass. The other is a common method. Can be directly inherited using the quilt class.
Construction Method: Abstract class cannot be instantiated, but it has a construction method, function: In order to let subclass instantiation when used.

Abstract cannot coexist with those keywords?
Private, external directly inaccessible. Subclasses are also inaccessible, and the method of Abstrac modification is to let subclasses inherit and override the
Static: The method It modifies can be called by the class name, which is meaningless for abstract methods.
Final: The method that it modifies cannot be overridden. So it conflicts with abstract.

Interface: When all methods in a class are abstract, it is not necessary to define an abstract class, which is defined as an interface.
Features: 1, is the rule of external exposure, 2, is the expansion of functions, 3, reducing the coupling of the program.
Member Features: 1, only member variables and member methods; 2, member variable default modifier public static final (no order required), 3, member method default modifier public abstract;

Classes and classes: can only be single-inheritance. Extends
Interfaces and interfaces: can be single-inheritance, or multiple-inheritance. Extends
Classes and interfaces: can be single or multi-implementation. Implements

interface and abstract class differences: 1, the abstract class can only be inherited, the interface can be implemented more than 2, the member type is different, 3, the abstract class is defined in the architecture of the common content. The extension function of the object is defined in the interface, and 4, the abstract class is inherited to represent the relationship of ownership. The interface is implemented to represent the relationship of "like a". xx like yy in one of the

Both abstraction and interface are

Java09. Object-oriented. Polymorphism, abstract class, interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.