Java hands-on brain

Source: Internet
Author: User

1. Run the Testinherits.java example, observe the output, and note the call relationship that summarizes the constructor method between the parent class and the subclass modifies the code of the parent constructor method, explicitly calls the other constructor of grandparent, and note whether the calling code is the first sentence, Significant Impact!

On the base class construction method called by supper must be the first statement in the constructor of a subclass

See Explorationjdksource.java example A Class A is defined in this example, and it does not have any Members: Class A {} Example directly outputs the object created by this class see Explorationjdksource.java example a Class A is defined in this example, and it does not have any members: the Class A {} example directly outputs objects created by the classes

class file compilation can be



In the previous instance, the main method calls the

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

Public String toString () {

Return GetClass (). GetName () + "@" +integer.tohexstring (hashcode);

}

The Hashcode method is a local method, with the JVM designer implementing

Let's take a look at a piece of code (example Fruit.java): 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 ());}    } Note the last sentence, a string and an object "add" to get the following 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". In order to return a meaningful
Information

, subclasses can override ToString () ZZ
1 in the "+" operation, when any one object is associated with a string object , the ToString () method is called implicitly when connected, and,<br> This method returns "class name @ + hashcode" by default. To return meaningful information, subclasses can override the ToString () method.
1 2 3 方法覆盖要求子类与父类的方法一模一样,否则就是方法重载(overload)!请自行编写代码测试以下特性:在子类中,若要调用父类中被覆盖的方法,可以使用super关键字。

Class fulei{

public int Jianfa (int a,int b) {

int s;

S=a-b;

return s;

}

}

Class Add1 extends fulei{

public int Jianfa (int a,int b) {System.out.println ("Call the parent class" +SUPER.JIANFA (3,4));

int s;

S=a-b;

return s;

}

}

public class Testinherits {

public static void Main (String args[]) {

Add1 C = new Add1 ();

System.out.println ("Call subclass" +c.jianfa (3,4));

}

}

Java hands-on brain

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.