Java super keyword, super keyword

Source: Internet
Author: User

Java super keyword, super keyword

Super Keyword:

In java, super is used to reference the components of the base class.

Program code:

class FatherClass{    public int value;    public void f(){        value = 100;        System.out.println("FatherClass.value:"+value);    }}class ChildClass extends FatherClass{    private int value;    public void f(){        super.f();        value=200;        System.out.println("ChildClass.value:"+value);        System.out.println(value);        System.out.println(super.value);    }}public class TestInHerit{    public static void main(String args[]){        ChildClass cc = new ChildClass();        cc.f();    }}

Output result:

Memory Distribution:


What is the super keyword in java?

Super usage
:

Public class Pelope {

Int high;
Public Pelope (String name, int age, String sex ){
// And so on.
}

Private void setHigh (int hight ){
This. high = high;
}
// Etc.
}

Public Boy extends Pelope {
Public Boy (String name, int age ){
Super (name, age, sex); // here is equivalent to the constructor in the parent class.

}
// The primary method or variable in the parent class can be used in this way, super. setHigh (),,
Super. variable ;;
I don't need to write much. That's almost the case ,,

There are also some applications in the generic class. Let's take a look at them by yourself.

Haha

Functions and usage of the keyword "this" and "super" in java

This keyword
1) inside the class, you should have seen this. xxx (), this. xxx usage,
This indicates the object of this class, such
Public class {
Private String name;
Public void setName (String name ){
// This indicates an object of.
// When your instance is A, A a1 = new A (); this is the a1, but this is used internally, while a1 is used externally
// A a2 = new A (). In this case, inside a2, this indicates a2.
This. name = name;
}
}

2) The second usage of this is used in constructors.
When you want to call other constructors of your own in the constructor, use this
This must be placed in the first line
Public class {
Private String name;
Public (){
This ("no name ");
}

Public A (String name ){
This. name = name;
// Other codes
}
}

Super keyword
1) explicitly call the method of the parent class
When inheriting from a class, both the subclass and the parent class have a method of the same name, that is, the subclass overwrites the method of the parent class, but wants to call the method of the parent class, so we need to use super. There is a good example when inheriting the swing class.
Public class MyPanel extends JPanel {
@ Override
Public void paint (Graphics g ){
Super. paint (g); // If super is not used, your painting method will be called.
}
}

2) It is used in the constructor. Like this, super can also be used in the constructor. this is to call other constructor of its own, so super is to call the constructor of its parent class.
-------------------------------------------------------

If super and this are used in constructors, the former indicates that the parent class is called early enough, and the latter indicates that other constructors of this class are called, both of them must be the first line written in the constructor.

Public class Person {
Private String name;
Private int age;
Public Person (){
Name = "";
Age = 0;
}

Public Person (String n, int ){
Name = n;
Age =;
}
}

Public class Student extends Person {
Private String id; // student id

Public Student (String name, int age ){
Super (name, age); // It must be written in the first line. The subclass cannot directly access the private attributes of the parent class. Therefore, you can call the constructor class of the parent class to initialize the attributes.
}

Public Student (String id, String name, int age ){
... The remaining full text>

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.