Interfaces and inheritance

Source: Internet
Author: User

1. Construction method invocation under inheritance conditions

(1) Source code

Package demo1;

A first-level invocation, when constructed, calls the constructor of the base class;

Super

Constructor call must be the first statement in a constructor

class Gradeparent

{

Public Gradeparent ()//default constructor

{

System. out. println ("Gradeparent Created");

}

Public Gradeparent (String string)//overloaded

{

System. out. println ("Gradeparent created.string" +string);

}

}

class Parent extends gradeparent

{

Public Parent ()

{

Super ("Hello");

System. out. println ("Parent Created");

}

}

class Child extends Parent

{

Public Child ()

{

System. out. println ("Child Created");

}

}

Public class testinherits {

Public Static void Main (String args[])

{

Child C=new child ();

}

}

(2) Design ideas

First define a Gradeparent class, and then define a parent, directly inherit gradeparent, define a child class, directly inherit the parent class, in order to see the order of calls, so in each constructor there are output statements to determine which first output, Which one to call first; if there are no parameter constructors and a parameter constructor in the class, the default is the parameterless constructor, and if you want to invoke the constructor of the parent class, you can use super.

(3) Program results

The first is that there is no use of super, the second uses super,super () in parentheses with parameters, so the constructor of the base class with parameters is called.

(4) Conclusion

The call relationship of the constructor method between the parent class and the subclass modifies the code of the parent constructor method, explicitly calling the other constructor of grandparent, and notice whether the calling code is the first sentence, and the impact is significant!

Calling the base class construction method through super must be the first statement in the subclass construction method. The way to inherit base classes is to add extends keywords.

2. Why do I have to call the constructor method of the parent class before the constructor of the subclass is run? Can you turn around? Why not turn around

The function of the constructor is to initialize, just like kinship, only with the father, there will be children, can not be reversed.

3. What is a "immutable class"?

When an object of immutable class is created, the properties of this object cannot be changed, and new subclasses cannot be derived from this class. String is a typical example. Immutable "Classes" can be conveniently and safely used in multithreaded environments, and accessing them can provide high performance without locking.

(1) Source program

Public Final class Address

{

Private Final String detail;

Private Final String postcode;

Initializing two instance properties in a constructor method

Public Address ()

{

this. Detail = "";

this. Postcode = "";

}

Public Address (string detail, string postcode)

{

this. detail = detail;

this. Postcode = postcode;

}

Provides getter methods for only two instance properties

Public String Getdetail ()

{

return this. detail;

}

Public String Getpostcode ()

{

return this. Postcode;

}

Override the Equals method to determine whether two objects are equal.

Public boolean equals (Object obj)

{

if (obj instanceof Address)

{

Address AD = (address) obj;

if (this. Getdetail (). Equals (Ad.getdetail ()) && this. Getpostcode (). Equals (Ad.getpostcode () ))

{

return true;

}

}

return false;

}

Public int hashcode ()

{

return Detail.hashcode () + Postcode.hashcode ();

}

}

(2) Design ideas

Defines a class that does not allow inheritance, there are two parameters in the class, one parameter with no parameters, an overload of the method, two instance properties in the class, and a get function to get the instance properties, and the Equals method to determine if the object is equal

4. Why do you output such a result

(1) Source program

Package demo1;

Public class Explorationjdksource {

/**

* @param args

*/

Public Static void Main (string[] args) {

System. out. println (new A ());

}

}

class a{}

(2)

(3) Analysis of results

In the preceding example, the main method actually calls:

public void println (Object x), which internally invokes the ValueOf method of the String class.

The Object.ToString method is also called inside the ValueOf method:

Public String toString () {

Return GetClass (). GetName () + "@" +

Integer.tohexstring (Hashcode ());

}

The Hashcode method is a local method that is implemented by the JVM Designer:

public native int hashcode ();

5. Method overrides

(1) Source code

Design of abstract classes and interfaces

Chen Xiaoyang 2016.11.11

Public class Shape {

Public Static void Main (String args[])

{

Rectangle a=New Rectangle (3,4);

System. out. Print ("area" +a.getarea ());

}

/*void Showarea ()//abstract method, seeking area and display

{

}*/

}

Interface diagarea//Defining Interface Classes

{

double getdiagonal ();//Find the diagonal length method

double Getarea ();//Seek area

}

class Rectangle implements diagarea//rectangular class, implements interface

{

int a,b;//indicates length and width

Rectangle ()

{

a=0;b=0;

}

Rectangle (int A1,int B1)

{

A=A1;B=B1;

}

Public double getdiagonal ()//cover, seek diagonal length

{

return Math. sqrt (A*A+B*B);

}

Public double Getarea ()//cover, seek area

{

return a*b;

}

}

class Square extends rectangle//Square inherits the rectangle class

{

int x;//side of Square

constructor to initialize the function.

Square ()

{

x=0;

}

Square (int aa)

{

A=AA;

}

Public double getdiagonal ()//Seek diagonal

{

return Math. sqrt (a*a*2);

}

Public double Getarea ()//Calculate area

{

return a*a;

}

void display ()//show edge length, area, diagonal length

{

System. out. println ("The side length, area and diagonal of the Square are:");

System. out. println (x+ "" +getarea () + "" +getdiagonal ());

}

}

Class Circle

(2) Design ideas

Define an interface in which there are functions that implement interfaces, implement overrides inside, and then define variables in the main method, calling the results of the overwrite.

(3)

(4) compilation error, precautions

The allowed access scope of the override method cannot be less than the original method. The override method throws an exception that cannot be more than the original method. Declaring the final method does not allow overrides. For example, the GetClass () method of object cannot be overridden. Static methods cannot be overwritten, and public is used when overwriting.

Interfaces and inheritance

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.