Second, Java object-oriented (8) _ Inheritance thought--super keyword

Source: Internet
Author: User

2018-05-01

Super keyword

What is super?

This: Represents the current object itself, or an instance of the current class, through which all methods and properties of this object can be called.

Super: Represents the parent class object of the current object. ( is a reference to the parent class object in the Subclass object )

use super in the Java class to refer to the composition of the parent class, use this to refer to the current object , and if a class inherits from another class, when we new the instance object of the subclass, there will be a parent class object inside the subclass object.

How do you refer to the parent object inside? Using Super to refer to, this refers to a reference to the current object, and super is a reference to the parent object inside the current object.

code example:

1 //Birds2 classBird3 {4      Public voidFly () {5SYSTEM.OUT.PRINTLN ("Free Flight");6     }7 }8 //Penguin9 Ten classPenguinextendsBird One { A      -      Public voidFly () { -System.out.println ("The angel of the Broken Wing"); the     } -      Public voidsay () { -SYSTEM.OUT.PRINTLN ("I Want to sing hahaha"); -         Super. Fly ();//use Super as the reference object of the parent class object to invoke the Fly () method inside the parent class object +     } - } +  A //Super keyword at classSuperdemo - { -      Public Static voidMain (string[] args) -     { -         //Create a Penguin object -Penguin p =NewPenguin (); in         //call the Fly method - P.say (); to     } +}

Output Result:

Memory Analysis:

Reference blog:

Https://www.cnblogs.com/danbing/p/5034108.html

Https://www.cnblogs.com/ahfz/p/5890631.html

Second, Java object-oriented (8) _ Inheritance thought--super keyword

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.