Java Interfaces and inheritance

Source: Internet
Author: User

1 public class Fruit

2 {

3

Public String toString ()

4

{

5return "Fruit toString.";

6}

7

8 public static void Main (String args[])

9 {

Ten Fruit f=new Fruit ();

System.out.println ("f=" +f);

System.out.println ("f=" +f.tostring ());

13}

14}

2) Results:

3) Results Analysis:

Note the penultimate sentence, a string and an object "add" to get the following results?

The fruit class overrides the ToString method of the object class. In the "+" operation, when any object is connected to a string object, its ToString () method is called implicitly, and by default, this method returns "class name @ + Hashcode".

To return meaningful information, subclasses can override the ToString () method.

Second, hands-on experiment: Construction method invocation under inheritance condition

1) Source code:

1 public class Testinherits {2  3 public     static void Main (string[] args) {4         //TODO auto-generated method Stu B 5  6 child         C = new Child (); 7     } 8  9}10 one class GrandParent12 {public     grandparent ()     {15
   
    system.out.println ("Grandparent created.sting:");     }17 public     grandparent (string string) @     {19         System.out.println ("Grandparent created.sting ...:" + string);     }22-Class Parent extends GrandParent24 {public     parent () +         ("MK")         System.out.println ("Parent Created"),         //super ("Hello.grandparent"),}31}32     class Child extends Parent34 {         System.out.println ("Child Created") () ()     
   

2) Results:

3) Results Analysis:

Calling the base class construction method through super must be the first statement in the subclass construction method.

4) Why do I have to call the constructor method of the parent class before running the constructor of the subclass? Can you turn around? Why can't it be reversed?

cannot be reversed. Subclasses inherit from the parent class, so subclasses have properties and methods of the parent class, and if you do not call the parent class's constructor, you cannot initialize the properties defined in the parent class, that is, you cannot allocate memory space to the properties of the parent class, and if the properties of the parent class do not allocate memory space, the subclass accesses the property of the parent class

Two Discover the mysteries of technology

1. Code

public class Explorationjdksource {

/**

* @param args

*/

public static void Main (string[] args) {

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

}

}

Class a{}

2. Results

3.

The truth:

• 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 ();

Three Hands on the brain

• In subclasses, to invoke the overridden method in the parent class, you can use the Super keyword.

public class Super

{

public static void Main (String args[])

{

Child c=new Child ();

C.show ();

}

}

Class Parent

{

public void Show ()

{

System.out.println ("parent");

}

}

Class Child extends Parent

{

public void Show ()

{

SYSTEM.OUT.PRINTLN ("Child");

Super.show ();

}

}

Four Method override (Override)

public class Fruit

{

Public String toString ()

{

Return "Fruit toString.";

}

public static void Main (String args[])

{

Fruit f=new Fruit ();

System.out.println ("f=" +f);

System.out.println ("f=" +f.tostring ());

}

}

• Results

• Conclusion: In the "+" operation, when any object is connected to a string object, its ToString () method is called implicitly, and by default, this method returns "class name @ + Hashcode". To return meaningful information, subclasses can override the ToString () method.

• Important: Method overrides require that the subclass be exactly the same as the parent class, otherwise the method overload (overload)!

Java 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.