Java series Learning (9)-polymorphism and java series learning Polymorphism

Source: Internet
Author: User

Java series Learning (9)-polymorphism and java series learning Polymorphism

1. final keywords

(1) The final meaning can be used to modify classes, methods, and variables.

(2) features:

   

A: The class it modifies cannot be inherited.

B: The modification method cannot be overwritten)

C: The variable it modifies. This variable is actually a constant.

[Expansion]

① Literal constant: "hello", 10, true

② Custom constant: final int x = 10;

(3) final interview:

   

A: The problem of modifying local variables using final

① Basic type: the value of the basic type cannot be changed.

② Reference type: the address value of the reference type cannot be changed, but the heap memory value of this object can be changed.

B: initialization time:

① Assignment only once

② Common assignment

2. Polymorphism

(1) the same object is displayed in different States at different times.

(2) Prerequisites for Polymorphism:

   

A: There is an inheritance or implementation relationship.

B: Method override

C: A parent class or parent interface references a subclass object.

(3) classification of Polymorphism

   

A: Specific Class Polymorphism

B: abstract class Polymorphism

C: interface Polymorphism

(4) member access characteristics in Polymorphism

   

A: member variables

[Compile to the left, run to the Left]

B: Constructor

[Parent class structure is accessed by default for subclass construction]

When creating a subclass object, the system first accesses the constructor of the parent class to initialize the data of the parent class.

C: Member Method

[Compile to the left, run to the right]

D: Static Method

[Compile to the left, run to the Left]

E: Why?

[Because the Member methods are overwritten, static methods are related to classes]

(5) benefits of Polymorphism

   

A: improve code Maintainability (inheritance)

B: improve code Scalability (polymorphism)

(6) Disadvantages of polymorphism:

   

The parent class cannot use the special functions of the subclass.

Performance: A child can be used as a parent, and a parent cannot be used as a child.

(7) transformation in Polymorphism

   

A: upward transition (from child to parent)

[Person p = new Student ()]

B: downward Transformation (from parent to child)

[Student s = (Student) p]

3. abstract class

(1) An example is provided to understand abstract classes.

   

Both cats and dogs are animals. Based on inheritance, we extract a parent class Animal, which is called by both cats and dogs, that is, their commonalities,

You can define a method in the Animal class. But the problem arises,

The cat is meow, and the dog is bark. In the method body of the parent class, how should we write it.

We can define only one method in Animal, but there is no method body. Let the subclass implement the specific method.

The method without a method body is called an abstract method. This class is called an abstract class.

(2) Characteristics of abstract classes

   

A: abstract classes and abstract methods must be modified using the keyword abstract

B: abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes.

C: The abstract class cannot be instantiated. It must be implemented by subclass.

D: subclass of the abstract class

① Is an abstract class

② Is a specific class. This class must override all abstract methods of the abstract class.

(3) Characteristics of abstract class members:

   

A: member variables

[With variables and constants]

B: Constructor

[Constructor]

C: Member Method

[Abstract and non-Abstract]

D: Example

(4) several minor issues with abstract classes

   

A: abstract classes and constructor methods cannot be instantiated. What is the purpose of constructor?

Used to initialize the sub-class to access the parent class data

B: If a class has no abstract method but is defined as an abstract class, what is the purpose?

To prevent object Creation

C: Which keywords cannot coexist in abstract?

① Final Conflict

② Private conflict

③ Static is meaningless

4. Interface

(1) using examples to understand Interfaces

   

Airplanes and birds fly, but they belong to different things, so they cannot use abstract classes,

However, they all have the flying function, so you can consider using interfaces at this time.

(2) interface features:

   

A: The interface is modified with the keyword interface.

B: class implementation interface modified with implements

C: The interface cannot be instantiated.

D: interface implementation class

① Is an abstract class

② Is a specific class

(3) features of the interface members

   

A: member variables

[Can only be a constant, default modifier: public static final]

B: Constructor

[No constructor]

Q: If there is no parent class, how can I initialize the Child class of the implementation interface?

A:

C: Member Method

[Can only be abstract. The default modifier is public abstract]

(4) classes and classes, classes and interfaces, interfaces and interfaces

   

A: Class and Class: inheritance relationship. It can only be A single inheritance and can be inherited at multiple layers.

B: class and interface: Implementation relationship, which can be implemented or implemented more. You can also implement multiple interfaces when inheriting a class's colleagues.

C: interface and interface: inheritance relationship, which can be a single inheritance or multiple inheritance

(5) When to use abstract classes and interfaces

   

A: The abstract class is inherited to reflect the relationship between xx is a XX. Abstract Classes define the common functions of the inheritance system.

B: When the interface is implemented, the relationship between xx like a XX is displayed. The interface defines the extended functions of the inheritance system.

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.