Java OOP Part 1

Source: Internet
Author: User

1. in inheritance, for example
Class

{

Void show (){}

}

Class B extends

{

Static void show () {}// this is incorrect because the non-static method of the parent class cannot be overwritten as a static method in the subclass during inheritance.

 

}

 

2 Class

{

Int I;

A (int I)

{

This. I = I * 2;

}

}

 

Class B extends

{

Public static void main (string [] ARGs)

{

B = new B (2 );

}

B (int I)

{

System. Out. println (I );

}

}

 

In this example, because subclass B inherits a, when executing the constructor of the subclass, if a constructor of the parent class is not explicitly specified in the subclass, will first execute the non-parameter constructor of the parent class, but class A does not have such a function, so a compilation error occurs. therefore, you can add super (2 );

 

3

A static method cannot access instance (non-static) variables
Int x = 12;

Static void ()

{

Output X; // Error

}

Static Method B cannot access non-static method
Void go ()

Static void domore ()

{

Go ();

}

C static method can access the Jingtai method or variable
Static int X;

Static void ()

Static void B ()
{

Access X;

A ();

}

 

4. Static methods must be implemented, and abstract is an enemy, such
Static abstract void () {...} is incorrect

5. The super keyword cannot be found in static methods, because Super is related to the instance of a specific class and is in conflict with static.

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.