Java Second week study diary

Source: Internet
Author: User

day011. Abstract class

(1) Application Scenarios for abstract classes:

When we describe a class of things, we find that there is a certain kind of behavior, but this behavior is not specific at the moment, then we can extract the statement of this behavior, but not to achieve this behavior, when this behavior we call as abstract behavior, we need to use abstract class.

(2) The details of the abstract class should be noted:

1. If a function does not have a method body, the function must use the abstract modifier to modify the function to an abstract function.

2. If an abstract function is present in a class, then the class must also use an abstract adornment.

3. If a non-abstract class inherits an abstract class, all the abstract methods of the abstract class must be fully implemented.

4. Abstract classes can exist in a non-abstract way, or there can be abstract methods.

5. Abstract classes may not have abstract methods, but they are generally not so used, just syntax support.

6. An abstract class cannot create an object.

Question: Why can't an abstract class create an object?

Because abstract classes are abstract methods, it makes no sense to invoke abstract methods using abstract objects if an abstract class is to create an object.

7. Abstract classes are constructors, whose constructors are provided to subclass objects to initialize the properties of the parent class.

(3) Benefits of abstract classes

Mandatory subclasses must implement the specified method.

Naming conventions for constants: Capitalize all letters, separating words from words with underscores.
Abstract cannot work with the following keywords to modify a method:
1. Abstract cannot work with private to modify a method.
2. Abstract cannot work with static to modify a method.
3. Abstract cannot be combined with final to modify a method.

2. polymorphic

An object has many forms. (The reference-type variable of the parent class points to the object of the child class)

Or an interface reference type variable that points to an object of an interface implementation class

Prerequisites for polymorphism: inheritance or implementation of relationships must exist

Animal a = new dog ();

Polymorphism to note the details:

1. In polymorphic cases, when the child parent class has a member variable of the same name, the member variable of the parent class is accessed.

2. In polymorphic cases, when the child parent class has a non-static member function with the same name, the member function of the child class is accessed.

3: In polymorphic cases, when a child parent class has a static member function with the same name, the member function of the parent class is accessed.

4. In polymorphic cases, you cannot access members that are specific to subclasses.

Summary: In polymorphic cases, when the child parent class has a member with the same name, it accesses the members of the parent class, and only when the non-static function with the same name accesses the child class.

Compile look to the left, run does not necessarily look to the right.

Compile to the left: when compiling, the Java compiler checks to see if the class that the reference type variable belongs to has the specified member, and if it does not compile the error immediately.

Applications for polymorphism:

1. When polymorphic is used for formal parameter types, more types of data can be received.

Requirement 1: Define a function that can receive any type of graphic object and print the area and perimeter of the graphic.

2. When polymorphic is used to return a value type, you can return more types of data.

The benefits of polymorphism: Improved code scalability

3. Interface:

The definition format of the interface:

Interface interface Name {

Member variables

member functions

}

Function of Interface: program decoupling, defining constraint specification, expanding function

Interface to note the details:

1. The member variables in the interface are all bright, the default modifier is public static final

2. The methods in the interface are all abstract methods, the default modifier: Public abstract

3. Interfaces cannot create objects

4. The interface is not a constructor function

5. When a non-abstract class implements an interface through implements, it must implement all the methods in the interface

Relationships between interfaces and classes: implementing relationships

Note: A class can implement multiple interfaces

Relationship between interface and interface: inheritance relationship

An interface is one that can inherit multiple interfaces.

Java Second week study diary

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.