Knowledge points about inheritance, classes, polymorphism, and interfaces in Java

Source: Internet
Author: User

Inherited

Meaning: in object-oriented programming, you can create a new class by extending an existing class and inheriting the class's properties and behavior .

Pros : 1) reuse of code; 2) subclasses extend the properties and methods of the parent class; 3) The properties and methods of the parent class are available for subclasses; 4) design the application becomes simpler

Implementation of Inheritance:

  Keyword extends: inherit his Ted class, can inherit public,protected

 Public class extends Employee


Super Keyword:

  Function: Call the overridden method in the parent class

Note: The parent class constructor is the first executable statement, and by default the class invokes the parent class's parameterless constructor, and the subclass constructor initializes the parent class constructor first.

Format: Subclass the Parent class constructor method (Super. Method name ();) when the constructor in the parent class is called (Super (return value 1, return value 2 ...) );)

1) Modifier class: Cannot be inherited

2) Modification method: The final method cannot be rewritten

3) modifier variable: Variable value cannot be modified

Advanced Concepts for classes

Access modifiers:

1) Open access level: public (can be accessed by any other object)

2) Protected level: protected (can be accessed by classes in the same package or by subclasses of other packages)

3) Default access level: No modifiers (can be accessed by other classes in the same package)

4) Private access level: Private (only the class itself can be called)

Packaging:

  

Static initialization blocks:

static{{

Statement//Non-static

}         }

Instance initialization block, which executes once on each instantiation of the object of the class, before the constructor

Execution Order: 1) The corresponding constructor in the subclass is called

2) perform a call to Super, control the process to jump back to the subclass constructor

3) After the parent class constructor executes, the control flow jumps back to the subclass constructor

4) instantiate initialization block execution before any statements after super () in the subclass constructor are executed

5) Execute the statement after Super () in the subclass constructor

Inner class:

  Static Inner class:

class outer{    staticclass  inner{    }}

member Inner class:

class outer{    class  inner{    }}

Local inner class:

void f () {    class  inner{          void g () {        }}}    

Anonymous inner class:
The name of the new class or interface () {

The body of an anonymous inner class

}

Polymorphism and abstraction

Polymorphic:

  Meaning: Multiple states in different situations

Classification: Static polymorphism (method overloading), dynamic polymorphism (in a running system to determine the object of a method)

Step: 1) write the parent class derived subclass

2) overriding a parent class method in a subclass

3) Use Polymorphism (reference subclass object with parent data type)

4) Call subclass override method implementation

  instanceof keyword: used to determine the true type of the runtime object (reference instanceof class name)

Abstract

Abstract Keyword:

Before class (abstract class), cannot instantiate the parent class object, need to be inherited to implement

Abstract methods: Only method signatures, no method principals, classes must also be declared as abstract classes, and any subclass must override abstract methods, unless the subclass itself is abstract

Interface

  interfaces are different from classes : they cannot be instantiated, they do not contain any constructors, all methods are abstract, cannot contain instance member variables, cannot be inherited by a class, can be implemented by only one class, and inherit multiple interfaces

Interface declaration

  keyword :interface

  Format:

 Public Interface Interface Name {    // any number of final,static member variables     // any number of method declarations }

Interface Properties: 1) The interface is abstract by default; 2) Each method in the interface is abstract by default; 3) The default access level is public

Interface implementation

Keyword:implements

Format: public class class name extends parent class name implements interface name

Multiple inheritance between interfaces and interfaces, only one parent class can be inherited

Using interface principles: 1) Using interfaces to resolve multiple inheritance

2) Adding functionality to external classes

3) separating a class from its own characteristics and behavior depending on the optional features and behavior of the outside, with an object-oriented perspective, so that the class is as simple as possible

Interface Benefits: Separating the design from the implementation, hiding the implementation from the external (caller) (and usually the caller does not need to be concerned about implementation); interface-oriented programming is the core of OOP


Interface inheritance

 Define a Hockeylistener interface to inherit the Sportslistener interface:

 Public Interface extends sportslistener{    // implement all the methods in the interface }

Define a Sportslistener class to implement the Hockeylistener interface:

 Public class Implements Hockeylistener {    // Six methods also required }

Inherit multiple interfaces:

 Public extends sportlistener,eventlistener{    // need to implement a method in three interfaces }

Interface inherits Sportslistener and EventListener two parent interfaces

  

Knowledge points about inheritance, classes, polymorphism, and interfaces in Java

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.