Java object-oriented programming--the Nineth chapter polymorphism and abstraction

Source: Internet
Author: User
Tags abstract definition

9.1 polymorphic

In biology, it means that a creature or species can have many different forms or stages;

In OOP, polymorphism refers to the ability of an object to have multiple forms.

Polymorphism can be said to be the essence of object-oriented programming. One of the reasons why a polymorphic concept is introduced in Java is that it differs from C + + on the inheritance of classes. Single inheritance guarantees the simplicity and clarity of the inheritance relationship, but it is bound to have a great limitation in function.

Because the subclass object contains a complete parent class object, the methods and properties defined in the parent class variable are found in the subclass object, so a parent class variable is allowed to reference the subclass object, but not the other way, because the child class may have more methods and properties than the parent class.

When a polymorphic call is made to a child class object with a parent class variable, only methods and properties defined in the parent class variable can be called, and methods and properties specific to the subclass cannot be called.

A parent class variable can point to any subclass object, and when a polymorphic call method is called, the subclass overrides the method is called preferentially.

The two most critical benefits of polymorphism: polymorphic parameters and heterogeneous collections.

9.2 polymorphic Classification

Statically polymorphic, when compiled, the system can decide which method to call-overloading;

Dynamic polymorphism, in operation, the system can dynamically determine the object that the method points to-rewrite.

Bottom line: The same behavior, different ways of implementation.

9.3 instanceof

Determines whether a variable points to an object that matches a certain type. Format: Variable instanceof object

9.4 Object-oriented features: inheritance, encapsulation, polymorphism

9.5 Abstraction

If the method of a class is defined only and there is no specific implementation, then this method is called an abstract method. Use the keyword abstract definition. Classes that have abstract methods are called abstract classes, and you need to add the keyword abstract. Abstract methods of abstraction classes should be implemented by subclasses according to their own characteristics.

Abstract classes cannot be instantiated. Abstract class variables can only point to child class objects.

If a class inherits an abstract class, the class must implement all the abstract methods in the abstract parent class, or the class must be an abstract class.

The private method defaults to the FINA modifier and cannot be overridden by the quilt class. Abstract methods cannot be private, static, or final.

Java object-oriented programming--the Nineth chapter polymorphism and abstraction

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.