On java object-oriented (Abstract inheritance interface polymorphism)

What is inheritance?

When the same attributes and behaviors exist in multiple classes, these contents are extracted into a single class, so multiple classes do not need to define these attributes and behaviors, just inherit that class.

Multiple classes can be called subclasses, and a single class is called a parent class, superclass, or base class.

Subclasses can directly access non private properties and behaviors in the parent class.

Use the extends keyword to generate an inheritance relationship between classes.

Class subdemo extends demo {} / / subdemo is a child class and demo is a parent class

What are the benefits of inheritance?

• improve code reusability.

• making the relationship between classes is the premise of polymorphism.

Characteristics of inheritance

1. Java only supports single inheritance, not multiple inheritance.

2. Java supports multi-layer (RE) inheritance (inheritance system).

Considerations when using inheritance

• if there is a relationship between classes: is a, you can consider using inheritance.

• do not use inheritance in order to inherit some functions.

What's the difference between super and this?

Super is a keyword that represents the storage space ID of the parent class. (can be understood as the father's quotation)

The usage of super and this is similar.

This represents the reference of the object (whoever invokes it represents who); Super represents the reference of the current subclass to the parent class.

Usage scenario

• when a child parent class has a member with the same name, it can be distinguished with super; • When a subclass wants to call the parent constructor, you can use the super statement.

difference

1. Member variables

this. Variable -- Super of this class Variable -- of parent class

2. Construction method

this(...) -- Super (...) of this class-- Parent class

3. Membership method

this. Method name () -- super Method name () -- name of the parent class

super(); And this (); Both are in the first line of the constructor and cannot appear at the same time.

Method override (override)

As like as two peas in the subclass, the overlay operation, also known as rewriting or duplication, appears in addition to the permissions modifier, the permissions modifier is greater than or equal to private, the return value type, the method name and the parameter list are same.

The private method of the parent class is invisible to the child class, so it is impossible to rewrite the private method of the parent class.

Coverage considerations:

• when overriding, the permission of the subclass method must be greater than or equal to the permission of the parent method;

• static can only override static.

Usage scenarios covered:

When a subclass needs the function of the parent class and the subclass of the function subject has its own unique content, it can copy the methods in the parent class. In this way, it not only follows the function of the parent class, but also defines the unique content of the subclass.

What is the difference between method rewriting and overloading?

The method rewrites as like as two peas, except for the permission modifier, the return value type, the method name and the parameter list are the same.

Overloading is used when the method names are the same and the parameter lists are different (not related to the return value type) in the same class.

Usage of constructor in child parent class:

1. During the initialization of subclasses, first go back to the initialization of the parent class. Because there is a super () in the subclass's constructor by default. Subclasses need to use the member variables of the parent class. This initialization must be completed before subclass initialization. Therefore, the initialization of the parent class will be performed first during the initialization of the child class.

2. If the parent class does not have a parameterless constructor

• use super to call the parameterized construction of the parent class. Recommended method.

• use this to call other constructs of itself.

Static code block, construction code block and execution sequence of construction method:

Parent static code block → child static code block → parent construction code block → parent construction method → child construction code block → child construction method

Final keyword

Final is a keyword that can be used to modify classes, member variables, and member methods.

characteristic:

The class it modifies cannot be inherited.

The member variable it modifies is a constant.

The member method it modifies cannot be overridden by subclasses.

The constant definition of final modification generally has writing specifications. All letters of the constant name modified by final are capitalized.

Final modifies a member variable and must be initialized. There are two kinds of initialization

Display initialization;

Constructor initialization. However, two cannot be initialized together

Difference between final and private:

The final modified class can be accessed; Private can not modify external classes, but can modify internal classes (in fact, it is meaningless to privatize external classes).

Final modified methods cannot be overridden by subclasses; On the surface, the method modified by private can be overridden by subclasses. In fact, subclasses cannot see the private method of the parent class.

The variable decorated with final can only be assigned once during display initialization or constructor initialization, and cannot be changed later; Private modified variables are not allowed to be directly accessed or modified by subclasses or other classes in a package, but they can be changed and valued through set and get methods.

polymorphic

Concept:

Objects show different states at different times.

Prerequisites for polymorphism:

There should be inheritance or implementation relationship.

There should be method rewriting.

A parent class reference should point to a child class object.

Embodiment in the procedure:

A reference to a parent class or interface points to or receives its own subclass object.

Benefits and effects:

The existence of polymorphism improves the scalability and later maintainability of the program.

Disadvantages:

When calling a parent class, you can only call the methods in the parent class, not the special methods of subclasses, because you don't know what subclasses will inherit you in the future.

Polymorphic member characteristics:

Member variable: compile time: check whether there are called variables in the class to which the reference variable belongs;

Runtime: it also depends on whether the class to which the reference variable belongs has a called variable.

Whether the member variable is compiled or run, it depends on the class to which the reference variable belongs. Simply remember the member variable. Both compilation and run look to the left of the equal sign.

Member method: compile time: check whether there are calling members in the class to which the reference variable belongs;

Runtime: to see if there are any calling members in the class to which the object belongs. If a parent-child method with the same name appears, the method in the subclass will be run because the method has overriding properties.

Compile to the left and run to the right.

Static method: compile time: check whether there are called variables in the class to which the referenced variable belongs;

Runtime: it also depends on whether the class to which the reference variable belongs has a called variable.

Compile and run both look to the left of the equal sign.

You must not be able to convert the object of the parent class into a subclass type!

The reference of the parent class points to the child class object, which can be promoted or cast.

Polymorphism is a subclass object changing from beginning to end!

Abstract

Abstract is to abstract the common and essential content from multiple things.

Abstract class:

A method without a method body can be defined in Java. The specific implementation of this method is completed by subclasses. This method is called an abstract method, and the class containing the abstract method is an abstract class.

Origin:

Multiple objects have the same function, but the specific content of the function is different. In the extraction process, only the function definition is extracted, not the function subject, then only the function declaration, and the method without the function subject is called the abstract method.

Abstract class features:

Abstract methods must be in abstract classes;

Abstract methods and abstract classes must be modified by the abstract keyword;

Abstract classes cannot create objects with new because calling abstract methods is meaningless;

In order to use the abstract method in the abstract class, the subclass must copy all its abstract methods, and then establish the subclass object call; If the subclass only covers part of the abstract methods, the subclass is still an abstract class;

Abstract classes can have abstract methods or non abstract methods. Abstract methods are used for subclass instantiation;

If a class is an abstract class, then inherit its subclasses, either abstract classes, or override all abstract methods.

Special: abstract methods can not be defined in an abstract class. This is only to prevent the class from establishing objects.

Characteristics of abstract class members:

Member variable: can be a variable or a constant;

Construction method: there are construction methods;

Member method: can be abstract or non abstract.

Precautions for abstract classes:

Abstract classes cannot be instantiated. Why are there constructors?

As long as it is a class defined class, there must be a constructor. Functions in abstract classes are instantiated for subclasses.

A class has no abstract methods. Why is it defined as an abstract class?

Don't want to be inherited, don't want to be instantiated.

What keywords cannot abstract coexist with?

Final: if the method is abstracted, it needs to be overwritten, and final cannot be overwritten, so there is a conflict.

Private: if the function is private and the subclass cannot be accessed directly, how can it be overwritten?

Static: abstract methods can be called without an object and class name. There is no point in calling abstract methods.

Interface

An interface is a collection of abstract methods and constant values. In essence, an interface is a special abstract class. This abstract class only contains the definitions of constants and methods, without the implementation of variables and methods.

Format: interface interface name {}

The appearance of interface reflects "multi inheritance" in another form, namely "multi implementation".

Implementation

Format: class name implements interface name {}

characteristic:

Interface cannot be instantiated.

If a class implements an interface, it is either an abstract class or implements all the methods in the interface.

Characteristics of interface members:

The member modifier in the interface is fixed!

Member constant: public static final. The variables defined in the interface are global constants, and the modifier can only be these three keywords, which can be omitted. The constant name should be capitalized.

Member method: public abstract. The methods defined in the interface are abstract, and the two modifier keywords can be omitted.

Recommendation: always give modifiers manually.

Difference between inheritance and Implementation:

The relationship between classes is called inheritance: whether the class is abstract or non abstract, it can define non abstract methods. This method can be directly used by subclasses, and subclasses can inherit. You can only inherit in single layer or multiple layers. ((class))

There is an implementation relationship between classes and interfaces: because the methods in the interface are abstract, they can be instantiated only after being implemented by subclasses. It can be implemented alone or multiple; You can also implement multiple interfaces while inheriting a class. ((class) extends (class) implements (interface1,interface2…))

There is an inheritance relationship between interfaces: one interface can inherit another interface, add new properties and abstract methods, and interfaces can inherit more than one. ((interface) extends (interface1,interface2…))

Differences between abstract classes and interfaces:

Member variable

Abstract classes can have variables or constants

Interfaces can only have constants

Member method

Abstract classes can have non abstract methods or abstract methods

Interfaces can only have abstract methods

Construction method

-Abstract class has constructor - interface has no constructor

Relationship between class and abstract class and interface

The relationship between class and abstract class is to inherit extensions

The relationship between class and interface is to implement implements

Ideological characteristics of interface:

1. Interface is the rule of external exposure;

2. The interface is the function expansion of the program;

3. The appearance of interface reduces the coupling; (modular development is realized, rules are defined, and everyone implements their own modules, which greatly improves the development efficiency)

4. The interface can be used for multiple implementations;

5. Multiple unrelated classes can implement the same interface;

6. A class can implement multiple interfaces that are not directly related to each other;

7. Similar to inheritance, there is polymorphism between interface and implementation class.

Inner class

Defining a class in another class is called an inner class. The appearance of inner classes breaks the limitation of Java single inheritance again.

Access features:

Internal classes can directly access members of external classes, including private members.

If an external class wants to access the members of an internal class, it must create an object of the internal class.

Classification and commonness of internal classes:

generality:

The inner class is still an independent class. After compilation, the inner class will be compiled into an independent class Class file, but preceded by the class name of the external class and the $symbol.

Inner classes cannot be accessed in the normal way. The inner class is a member of the outer class, so the inner class can freely access the member variables of the outer class, whether private or not.

Member inner class

There are member variables and member methods in the external class. The internal class of a member takes the whole class as a member of the external class;

The inner class of a member is a class outside the method defined in the class;

The format of object creation is: external class name Internal class name object name = external class object Internal class objects;

The reason why the inner class can directly access the members of the outer class is that the inner class holds a reference to the outer class object: the outer class name this;

The modifiers that can be used for member inner classes are final, abstract, public, private, protected, and static

Static inner class

A static inner class is a member inner class plus the static modifier static, which is defined outside the methods in the class.

There are two scenarios for accessing static internal classes in external classes:

Accessing non - static members in static inner class in external class: * external class name Internal class name object name = external class name Internal object *, which needs to be accessed by creating an object;

Access the static members in the static internal class in the external class: you can also access them in the above format or directly use the external class name Internal class name Members.

Local inner class

A local inner class is a class defined in a method.

A method inner class can only be instantiated within the method that defines the inner class, and cannot be instantiated outside this method.

Method internal class objects cannot use non final local variables of the method in which the internal class is located.

The modifiers that can be used for inner classes of methods are final and abstract;

Method inner classes in static methods can only access external static members.

Anonymous Inner Class

Anonymous inner class is a simplified way to write an inner class. It is an anonymous object that creates a subclass of an external class or interface with content.

Premise:

An internal class can inherit or implement an external class or interface.

Format:

New external class name or interface name () {override method};

Generally, when the formal parameter of a method is an interface or an abstract class, and there are no more than three methods in the interface, the anonymous inner class can be passed as a parameter.

Content modified by different modifiers (independent of inner class)

Note that common rules are as follows:

• in the future, all classes are decorated with public. Moreover, in a java file, only one class is written.

• later, all member variables are decorated with private.

• in the future, all member methods are decorated with public.

If it is an abstract class or interface: public abstract +

• later, all construction methods are modified with public.

If the class is a tool class or a singleton class: the construct is decorated with private

Four permission modifiers

recommend:

• member variable private

• construction method public

• member method public

The above article on Java facing objects (Abstract inheritance interface polymorphism) is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>