Four. Java inheritance and polymorphism 2. Java Super keyword

Source: Internet
Author: User

The Super keyword is similar to this, which is used to represent an instance of the current class, and super is used to represent the parent class.

Super can be used in subclasses, by Dot (.) To get the member variables and methods of the parent class. Super can also be used in subclass subclasses, where Java can automatically trace back to the upper class.

The behavior of the parent class is called as if the behavior is the behavior of this class, and the invocation behavior does not have to occur in the parent class, it can automatically trace back to the upper class.

Features of the Super keyword:

    • Invokes a variable declared as private in the parent class.
    • Take a method that has already been overridden.
    • Represents the parent class constructor method as a method name.
Calling hidden variables and overridden methods
  1. Public class Demo{
  2. public static void main(String[] args) {
  3. dog obj = new dog();
  4. Obj. Move();
  5. }
  6. }
  7. Class Animal{
  8. private String desc = "Animals is Human ' s good friends";
  9. //must declare a getter method
  10. public String getdesc() { return desc; }
  11. public void move(){
  12. System. Out. println("Animals can Move");
  13. }
  14. }
  15. Class Dog extends Animal{
  16. public void move(){
  17. Super. Move(); //method of calling parent class
  18. System. Out. println("Dogs can walk and run");
  19. //Call the parent class to hide the variable by getter method
  20. System. Out. println("Please remember:" + Super. GetDesc());
  21. }
  22. }

Operation Result:
Animals can move
Dogs can walk and run
Please remember:animals is human ' s good friends

The move () method can also be defined in some ancestor classes, such as the parent class of the parent class, where Java is traceable and will always look up until the method is found.

Calling a hidden variable of the parent class through super, you must declare the Getter method in the parent class, because the data member declared as private is not visible to the child class.

Calling the parent class's constructor method

In many cases, the default constructor method is used to initialize the parent class object. Of course, you can also use super to display the constructor method that calls the parent class.

  1. Public class Demo{
  2. public static void main(String[] args) {
  3. dog obj = new dog("Floral", 3);
  4. Obj. Say();
  5. }
  6. }
  7. Class Animal{
  8. String name;
  9. public Animal(String name){
  10. this. Name = name;
  11. }
  12. }
  13. Class Dog extends Animal{
  14. int age;
  15. public Dog(String name, int age){
  16. Super(name);
  17. this. Age = Age;
  18. }
  19. public void say(){
  20. System. Out. println www.yxin7.com ("I am a cute puppy, my name is" + name + ", i" + age + "years old");
  21. }
  22. }

Operation Result:
I am a lovely puppy, my name is flower, I am 3 years old

Note: either super () or this () must be placed in the first row of the construction method.

It is worth noting that:

    • Invoking another constructor method in the constructor method, the invocation action must be placed at the starting position.
    • You cannot call a construction method within any method other than the constructor method.
    • Only one constructor method can be called within a constructor method.


If you write a constructor method that neither calls super () nor calls this (), the compiler automatically inserts a call into the parent class constructor method without parameters.

Finally note the difference between super and this: super is not a reference to an object, and a super is not assigned to another object variable, it is just a special keyword that instructs the compiler to call the parent class method.

Four. Java inheritance and polymorphism 2. Java 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.