Object-oriented features: inheritance, polymorphism

Source: Internet
Author: User
Tags access properties java reference

Inherited

• Why should there be inheritance?

When the same properties and behaviors exist in multiple classes, the content is extracted into a single class, so that multiple classes do not need to define these properties and behaviors, as long as they inherit that class.

• Multiple classes Here are called subclasses, and this class alone is called a parent class (base class or superclass). Can be understood as: Subclass is a parent class

• Class inheritance syntax rules:

Class Subclass extends superclass{}

Role:

The advent of inheritance increases the reusability of code

The emergence of inheritance allows a relationship between classes and classes, providing a precondition for polymorphism

Do not inherit just to get a feature in another class

• Subclasses inherit the parent class, inheriting the methods and properties of the parent class

In subclasses, you can use the methods and properties defined in the parent class, or you can create new data and methods.

• In Java, the inherited keyword is "extends", that is, the child class is not a subset of the parent class, but an "extension" to the parent class.

Rules about inheritance:

Subclasses do not have direct access to private member variables and methods in the parent class.

· Java only supports single inheritance and does not allow multiple inheritance

A subclass can have only one parent class

A parent class can derive multiple subclasses

Override of Method (override)

• Definition: In subclasses, methods that inherit from the parent class can be modified as needed, also known as the reset and overwrite of the method. When the program executes, the method of the child class overrides the method of the parent class.

• Requirements

The overriding method must have the same method name, parameter list, and return value type as the overridden method.

The overriding method cannot use more restrictive access than the overridden method.

The overridden and overridden methods must be static at the same time, or be non-static at the same time

The subclass method throws an exception that cannot be greater than the exception of the parent class overridden by the method

Keyword Super

• Use Super in the Java class to invoke the specified action in the parent class:

Super can be used to access properties defined in the parent class

Super can be used to invoke member methods defined in the parent class

Super can be used to call the constructor of a parent class in a subclass construction method

Note

Especially if the child parent has a member of the same name, you can use super to differentiate

Super's traceability is not limited to direct parent class

The use of super and this is similar, this represents a reference to this class of objects, super represents the identity of the memory space of the parent class

Calling the constructor of the parent class

• All constructors in subclasses access the constructor of the parent hollow parameter by default

• When a constructor with no empty arguments is in the parent class, the constructor of the subclass must either use the This (argument list) or super (argument list) statement to specify that the corresponding constructor in this class or parent class is called, and must be placed in the first row of the constructor

• If the constructor for the parent class or class is not explicitly called in the subclass constructor, and there is no parameterless constructor in the parent class, the compilation error

The difference between this and super

Object-oriented features: polymorphism

• Polymorphism: Is the most important concept in object-oriented, and is embodied in Java:

1. Overloading of methods (overload) and overrides (overwrite)

2. Object polymorphism--can be applied directly to abstract classes and interfaces.

· There are two types of Java reference types: compile-time type and run-time type. A compile-time type is determined by the type used when declaring the variable, and the run-time type is determined by the object that is actually assigned to the variable.

Polymorphism (polymorphism) If compile-time type and runtime type are inconsistent

• Object polymorphism-in Java, objects of subclasses can override the parent class's

A variable can have only one determined data type

A reference type variable may point to (reference) many different types of objects

Cases:

Person p = new Student ();

Object o = new Person (), variable o of type//object, objects that point to type of person

o = new Student (), Variable o of type//object, object to Student type

• Subclasses can be considered a special parent class, so a reference to the parent class type can point to the object of the subclass: transition Upward (upcasting).

• A reference type variable can no longer access properties and methods added to a subclass if it is declared as a type of the parent class, but the child class object is actually referenced.

Virtual method call (Vsan invocation)

• Normal method invocation

Person e = new person ();

E.getinfo ();

Student e = new Student ();

E.getinfo ();

• Virtual method Invocation (in polymorphic cases)

Person e = new Student ();

E.getinfo (); Call the GetInfo () method of the Student class

• Compile-time and run-time types

The compile-time E is the person type, and the invocation of the method is determined at run time, so the GetInfo () method of the student class is called. --Dynamic binding

Multi-State summary

Premise

There is a need to inherit or implement relationships

To have an overwrite operation

• Member Methods:

Compile time: To see if a method is called in the class to which the reference variable belongs

Runtime: Invokes an overriding method in the class to which the actual object belongs

• Member variables:

There is no polymorphism, just look at the class to which the reference variable belongs.

• Subclass Inherits Parent Class

If a subclass overrides the parent class method, it means that the method defined in the subclass completely overrides the same name method in the parent class, and the system will not be able to transfer the methods in the parent class to the subclass.

There is no such phenomenon with instance variables, even if the child class defines an instance variable that is exactly the same as the parent class, it is still not possible to overwrite the instance variable defined in the parent class

instanceof operator

X instanceof A: Checks if X is an object of class A, and the return value is Boolean.

Requires that the class to which x belongs must be a child class and a parent class, or a compilation error.

If x belongs to subclasses of Class A b,x instanceof a result is also true

Object type conversion (Casting)

• Casting of basic data types:

Automatic type conversion: Small data types can be automatically converted to big data

Coercion Type conversions: You can cast a large data type (casting) into a small data type

• Forced type conversions on Java objects are called styling

Type conversions from subclasses to parent classes can be done automatically

Type conversions from the parent class to the subclass must be implemented by styling (coercion type conversions)

The conversion between reference types without inheritance is illegal

You can test the type of an object instanceof operator before styling

Object class

The object class is the root parent class for all Java classes

• If the parent class is not indicated in the declaration of the class with the extends keyword, the default parent class is the object class

Main methods in the object class

= = comparison and Equals method

· ==

Base type comparison value: True if the value of two variables is equal.

Reference type comparison reference (whether point to the same object): = = only returns True if the same object is pointed to.

When you compare with = =, the data types on both sides of the symbol must be compatible (except for the basic data types that can be automatically converted), or compile errors;

equals () All classes inherit object, and the Equals () method is obtained. can also be overridden.

Reference types can only be compared, with the same effect as "= =", whether the comparison points to the same object

Format: Object1.equals (OBJECT2)

ToString () method

The ToString () method is defined in the object class and its return value is a string type, returning the class name and its reference address.

• The ToString () method is called automatically when a string is connected to another type of data

• You can override the ToString () method in a user-defined type as needed

• The ToString () method of the corresponding wrapper class is called when the basic type data is converted to a string type

Packing class

• The basic data type is packaged as an instance of the wrapper class--boxing

Implemented by the constructor of the wrapper class

Wrapper class objects can also be constructed from string parameters

• Get the basic type variable wrapped in the wrapper class object--unpacking

Called by the wrapper class. Xxxvalue () Method:

· After JDK1.5, automatic packing and unpacking are supported. But the type must match.

• Convert strings to basic data types

Implemented by the constructor of the wrapper class

Parsexxx (String s) static method by wrapping a class

• Convert basic data types to strings

Call the ValueOf () method of the string overload

More direct way: 5+ ""

Object-oriented features: inheritance, polymorphism

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.