Java Inheritance forms a program architecture

Source: Internet
Author: User

Therefore, a system to be used, direct access to the function of the parent class in the system to know the basic use of the system. When you want to use a system, you need to create objects. It is recommended that you establish the most child class object , because the most subclasses can not only use the functionality in the parent class. You can also use some of the features that are specific to subclasses.

Simply put: for the use of an inheritance system, consult the contents of the top-level parent class to create the object of the bottommost subclass.

3: constructor.

when the subclass constructor is found to run, the constructor for the parent class is run first. Why is it ?

Cause: The first line in all the constructors of a subclass has a stealth statement super ();

super (): represents the constructor of the parent class and is used for the constructor in the parent class that corresponds to the parameter. super (): is the constructor that calls the parent-class hollow argument.

Why do I need to call a function in the parent class when the subclass object is initialized? (why join the Super () in the first line of the subclass constructor)?)

Because subclasses inherit the parent class, they inherit the data from the parent class, so it is important to see how the parent class initializes its own data. So when the subclass initializes the object, it calls the constructor of the parent class, which is the instantiation of the subclass .

Note: all constructors in a subclass access the constructor of the null argument in the parent class by default, because the first row in each sub-class construct has a default statement super ();  

If there is no constructor for the null argument in the parent class, within the constructor of the subclass, you must specify the constructor in the parent class to access through the super statement.

if the subclass constructor uses this to specify the calling subclass's own constructor, the constructor that is called will also access the constructor in the parent class.

question:super () and this () can appear in the constructor at the same time.

Two statements can only have one definition in the first row, so only one can appear.

Super () or This (): Why must I define it in the first line?

because super () or this () is called the constructor, the constructor is used for initialization, so the initialization action is done first.

connector :★★★★★

1: is defined by the keyword interface .

2: The members that are contained in the interface, the most common are global constants, abstract methods.

Note: Members in an interface have fixed modifiers.

member variable:public static final

member Method:public abstract

Interface inter{

Public static final int x = 3;

Public abstract Void Show ();

}

3: There is an abstract method in the interface that indicates that the interface cannot be instantiated . the subclass of an interface must implement all the abstract methods in the interface before the subclass can be instantiated. Otherwise, the subclass is an abstract class.

The characteristics of polymorphism in the members of the Child parent class:

1, member variable: in polymorphism, the child parent class member variable has the same name.

At compile time: Refer to whether the referenced variable belongs to a class that has a calling member. ( no objects are generated at compile time, only syntax errors are checked )

Run time: is also a member of a class that refers to a reference -type variable that has a call.

A simple sentence: regardless of compile and run, member variables refer to member variables in the class to which the reference variable belongs.

Besides, it's easier to remember some: member variables --- compile and run all look = left.

2, member function.

Compile time: Refers to whether the referenced variable belongs to a class that has a calling method.

Run things: Refer to whether the object belongs to a class that has a calling method.

Why is that so? because in the child parent class, there is one attribute for the identical member function: Overwrite.

Simple sentence: The member function, compiled to see the reference type variable belongs to the class, run to see the object belongs to the class.

Simpler: member functions --- compile look = left, run see = right.

3, static function.

Compile time: Refer to whether the referenced variable belongs to a class that has a calling member.

Run time: is also a member of a class that refers to a reference-type variable that has a call.

Why is that so? because the static method does not belong to the object, it belongs to the class where the method resides.

Calling a static method reference is the static method in which class the reference is called.

Simply put: static functions --- compile and run all see = left.

-----------------------------------------------------------------------------------------------

------Java.lang.Object

object: the direct or indirect parent of all classes, Java believes that all objects have some basic common content, which can be extracted continuously upward, and finally extracted to a topmost class, defined in this class is the functionality of all objects.

Specific methods:

1,boolean equals (Object obj): used to compare two objects for equality, in fact, the internal comparison is two object addresses. If two objects are equal according to the Equals (object) method, calling the Hashcode method on each object in both objects must produce the same integer result ;

Depending on the properties of the object, it is different to judge whether the object is the same or not. Therefore, when defining a class, it is common to replicate the Equals method, and to establish the basis for determining whether the object is the same as this particular class.

public boolean equals (Object obj) {

if (! ( obj instanceof person))

return false;

Person P = (person) obj;

return this.age = = P.age;

}

2,string toString (): turn an object into a string ; The format returned by default: class name @ hash value = GetClass (). GetName () + ' @ ' + integer.tohexstring (hashcode ())

In order to make sense of the object's corresponding string content, it is possible to create a unique string representation of the class object by means of replication.

Public String toString () {

Return "Person:" +age;

}

3,class GetClass (): Gets the byte-code file object to which any object runtime belongs.

4,int hashcode (): returns the hash code value of the object. This method is supported to improve the performance of the hash table. The internal address of the object is converted to an integer.

usually Equals,tostring,hashcode, in the application will be replicated, to establish specific objects of the unique content.

Java Inheritance forms a program architecture

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.