Java polymorphic and abstract classes, interfaces

Source: Internet
Author: User

Polymorphism: Multiple forms of an object (inheritance is the basis for polymorphic implementations)

⑴ Reference polymorphism: A reference to a parent class can point to an object of this class; A reference to a parent class can point to an object of a subclass;

⑵ method Polymorphism: When creating this type of object, the method called is the method of this class; When a subclass object is created, the method that is called is the method that the subclass overrides or the inherited method;

⑶ a reference to a parent class cannot call a method that is unique to a subclass.

The two most critical benefits of polymorphism are: polymorphic parameters and heterogeneous sets;

The keyword instanceof is used to determine whether an object is an instance of a particular class.

Abstract class:

Abstraction is the ability to write abstract classes, which are classes that cannot be instantiated, and the keyword abstract is used to declare a class as an abstract class.

  1. Syntax definition: The abstract keyword is used to decorate before the class.
  2. Scenario: ① In some cases, a parent class simply knows what the subclass should contain, but does not know exactly how these subclasses implement these methods; ② abstracts an abstract class from multiple classes with the same characteristics, taking this abstract class as a template of subclasses, thus avoiding the arbitrariness of sub-class design;
  3. Role: Restriction subclasses must implement certain methods, but do not pay attention to implementation details;
  4. Using rules: ①abstract defines abstract classes, ② defined abstract methods, only declarations, no implementations, ③ classes containing abstract methods are abstract classes, ④ abstract classes can contain ordinary methods, or there can be no abstract methods; ⑤ Abstract classes cannot be created directly, you can define reference variables to point to child class objects.

Java polymorphic and abstract classes, interfaces

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.