Java EE Fundamentals 08

Source: Internet
Author: User

Java EE Fundamentals 81, Inheritance

Features: inherit the properties and methods of the parent class, single inheritance (multiple inheritance)

Properties: Replication of methods (overrides)

For example: people can keep dogs

people ------> dogs: Overall and partial (owned) relationships

Keyword:extends

Structure: class a extends Class B :classA inherits class B

things to be aware of in succession :

1. Do not inherit for the sake of inheritance

2. Private variables ( methods ) of the parent class cannot be inherited

3. The constructor of the parent class cannot be inherited

4. Subclasses call their own constructor method by default, calling the parent Class's parameterless constructor first

5. The call to Super must be the first statement in the constructor

6. Subclasses cannot inherit members that are not the default permissions in a package

When to use Inheritance:

The is-a relationship must be met between two classes in order to have an inheritance Relationship.

Two Super keyword

Super: Reference space pointing to the parent class object

the Super keyword works:

1. when a child class and parent class have a member variable with the same name, the member variable of the parent class can be called by super

2.Super can be used to invoke the constructor method of the parent class

Public Zi (string color, string name,int age) {

use Super to invoke the constructor of the parent class

Super (name,age); must be the first statement in a constructor

This.color = clolr;

}

The main things that Super uses:   

1. Call the constructor of the parent class with Super , must be the first statement in the constructor method

2.Super can only appear in the methods and construction methods of subclasses

3.Super and This cannot call a constructor method at the same time (because both must be in the first Statement)

The difference between Super and this:

1. The things represented are different:

This: Owner method Caller Object

Super: Represents a reference space for a parent class object

2. The use of the premise is different:

Super: Can only be used under inherited conditions

This: can also be used under non-inherited conditions

3. Call the constructor method:

Super: Call the parent Class's constructor method

This: Call the constructor method of this class

Two rewrite

method of re-writing by :

the parent Class's methods do not satisfy the implementation of the subclass, and the behavior of the parent class needs to be implemented by means of a replication method

The override of the method uses the note point:

1. Method overrides, The inheritance relationship must exist

2. Method overrides, method names and formal parameters must be consistent

3. Method overrides, the permission modifier for a subclass must be greater than or equal to the permission modifier of the parent class

4. Method overrides, The return value type of the subclass must be less than or equal to the return value type of the parent class

5. Method overrides, Subclass of exception type is less than equal to the exception type of the parent class

Basic Data Type: The hierarchy is ambiguous, so the basic data type is not established at point 4

Method Overloads:

Method name: Number of argument lists, type, and order

third, the key words

instanceof(instance type) keyword effect:

1. Used to determine whether an object belongs to a class

2.Instanceof: classes specified using the Precondition object have an inheritance relationship or implementation relationship

Final keyword:

1. If the basic data type variable is modified with final modification, it cannot be re-assigned, the first result is the final result (the final modified variable must be initialized )

2. You cannot assign a value to a reference data type variable if you use the final adornment to reference a data type

3. The method cannot be overridden if the final decoration method is used

4. If the class is final decorated, the class cannot be inherited

How to represent constants in final

Example: publicfinal static double PI = 3.14; variable names are capitalized

format: Public final static basic data type variable name

Abstract keyword:

classes that are modified by abstractions are called abstract Classes.

The Abstract modification method is called an abstraction method .

If a class inherits an abstract class, you must implement an abstract method in the abstract class

Use of the Abstract keyword:

1. If a method has no method body (the content of the curly brace is the method body), then this method must be modified by the abstract

2. If there is an abstract method in a class, the class must be abstract and

3. If a Non-abstract class inherits this abstract class, then all the abstract methods must be overridden in this non-abstract class

4. An abstract class can have a non-abstract method.

5. An abstract class can have a constructor method that allows subclasses to initialize variables in the parent class

6. Abstract classes cannot create objects

Cause: If you create an object, the object may be called to an abstract method, with no meaning

7. In an abstract class, there can be no abstract method.

Cases:

Abstract class Graph

{

Public Graph () {}; there is an parameterless construct by default

Abstract public void Area ();

}

Abstract cannot and: private,static,final Use

When to use the Abstract keyword :

When describing a thing, it is found that there is a certain behavior, but the behavior is not specific, This behavior is called abstract behavior, then use the abstract class

Four Interface

Example: pencil: Pencil with Eraser

two functions: 1. writing ----> pencil 2. Erase Word ----> Rubber

reality:Java is a single inheritance and cannot have two functions at the same time

Interface Definition Mode:

Use the interface keyword to represent the interface

define an interface for wiping words

Interface Eraser

{

int a = 10; the variables defined in the interface must be initialized, and the variables in the interface are final decorated

the method defined in the interface is not required to be implemented

public void Clear ();//the default is abstract , and the methods in the interface are abstracted methods

}

Implementing Interface Keywords implements:

Class Eraserandpencil implements Eraser, pencil{

the method in the interface must be implemented .

}

Java can support multiple implementations, one class can implement multiple interfaces, and multiple interfaces are separated by commas after implements

The role of the interface:

1. To expand the function

2. Decoupling Your Program

3. Defining constraints

Considerations for the use of interfaces:

1. The interface is a special class that can define properties and methods

2. Properties default to final decoration:publicstatic Final int-age = ten; is actually a constant

3. methods are abstract: public inabstract

4. Interfaces Cannot create objects

5. No construction method for interface

6. Interfaces are implementations of non-abstract classes to implement interfaces, you must implement the methods in the interface

Interface Eraser

{

int a = 10; a variable defined in an interface must be initialized with a variable in the interface with the final decorated

the method defined in the interface is not required to be implemented

public void Clear ();//the default is abstract , and the methods in the interface are abstracted methods

}

Class and interface Relationships: implementing Relationships

1. a non-abstract class implementation interface, must implement the method in the interface

2. an abstract class implementation interface, you can not implement the method in the interface, if a Non-abstract class inherits the abstract class, then this class needs to implement all the methods in this interface

An inheritance relationship exists between interfaces and interfaces that can inherit multiple interfaces

Java EE Fundamentals 08

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.