Java Learning notes (ongoing updates ...) )

Source: Internet
Author: User

1. In order to inherit, the general rule is to designate all data members as privateand all methods as Public

2. In the inheritance process, you need to declare "new class and Old class similar", with the keyword "extend" implementation. By doing this, you get all the fields and methods in the base class

3. In an inheritance relationship, if the exported class and base class have the same method, but you want to invoke the method inherited from the base class in the export class, you can use the keyword "super" to represent the superclass, and the expression "super.functionname ()" To call the base class version of FunctionName ( And, if there is no default base class constructor, or if you want to use a base class constructor with parameters, you must explicitly write the statement that calls the base class constructor with the keyword "super", with the appropriate argument list

classGame1 {Game1 (inti) {System.out.println ("Game1 Costructor"); }}classBoardgameextendsgame1{boardgame (inti) {        Super(i); System.out.println ("Boardgame Costructor"); }} Public classChessextendsboardgame{Chess () {Super(11); System.out.println ("Chess Costructor"); }     Public Static voidMain (string[] args) {//TODO auto-generated Method StubChess xchess =NewChess (); }}
/*output:
Game1 Costructor
Boardgame Costructor
Chess costructor*/

4. The "Is-a (is a)" relationship is expressed by inheritance, and "has-a (with one)" is used in combination to express

"Upward transformation" is the process of converting from a more specialized type to a more general type by exporting a class to a base class.

In OOP, this technique should be used with caution, despite repeated emphasis on "inheritance". A "combination" is a safe way to embed an object in a new class to implement the required functionality, but the new user sees only the interface defined for the new class, not the interface of the embedded object, and the member object hides the specific implementation from itself. Therefore, inheritance is necessary if you need to make an upward transformation from the new class to the base class, otherwise it is better to use a combination.

Java Learning notes (ongoing updates ...) )

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.