Java note (8) polymorphism, abstract classes, final keywords and interfaces

Source: Internet
Author: User

Polymorphism: determines the actual type of the referenced object during execution rather than during compilation, and calls the corresponding method based on the actual type.

Three necessary conditions for Polymorphism:

1. Inheritance required

2. Rewrite

3. parent class references to subclass objects

Abstract class

When an abstract keyword is used to modify a class, this class is called an abstract class. When abstract is used to modify a method, this method is called an abstract method.

Classes that contain abstract classes and methods must be declared as abstract classes. abstract classes must be inherited and abstract methods must be overwritten.

Abstract classes cannot be instantiated.

Abstract METHODS only need to be declared, rather than implemented

If the subclass does not implement the abstract method of the parent class, you can write the prototype of the abstract method again, or declare the subclass as an abstract class.

Final keywords

The final variable value cannot be changed.

Final member variables

Final local variables (parameters)

The final method cannot be overwritten.

The final class cannot be inherited.

Interface

An interface is a set of definitions of abstract methods and constant values.

Essentially, an interface is a special abstract class that only contains the definition of constants and methods, without the implementation of variables and methods.

The interface is defined as follows:

Public interface Runner {

Public static final int id = 1;

Public void start ();

Public void run ();

}

Multiple irrelevant classes can implement the same interface

A class can implement multiple irrelevant interfaces.

Similar to inheritance, There Is A polymorphism between interfaces and implementation classes.

The attribute declared in the interface is public static final by default; it can only be public static final

Only abstract methods can be defined in the interface. By default, these methods are public and can only be public.

Interfaces can inherit other interfaces and add new attributes and abstract methods.

The keyword of the implementation interface is implements.

An interface can inherit classes, but a class cannot inherit interfaces. A class can only implement interfaces.

Interfaces can be inherited from each other.

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.