Core Java - [super Keyword]

♠ Posted by Unknown in at 01:22

"super" keyword


The "super" keyword is used to refer the properties of super class. Means, if there are constructors in super class and sub class. Then the sub class constructor can call the constructor of super class using "super" keyword.  If any public member functions of super class can be called directly into subclass member function body the "super" keyword is useful to differentiate the super class or sub class function name.

//use of super keyword using constructor in sub class.
import java.io.*;
import java.lang.*;

class AExample
{
            private int a;
            public AExample(int x) //Constructor
            { a = x; }
            public void Put_Data()
            { System.out.println("In Super Class : " + a); }
};

class BExample extends AExample
{
            private int b;
            public BExample(int x, int y) //Constructor of Sub Class
            {
                        super(x); //Refer Super Class Constructor
                        b = y;
            }
           
public void Put_Data()
            {
                        super.Put_Data();
                        System.out.println("In Sub Class : " + b);
            }
};

public class  SuperExample
{
            public static void main(String[] args)
            {
                        BExample BObj = new BExample(5, 6);

                        BObj.Put_Data();
            }
}

1 comments:

Post a Comment