❮ Previous Next ❯

JAVA super keyword


  1. To access the super class property, we use super keyword in sub class.
  2. If we create an object to super class , we can access only the super class members but we cannot access the sub-class members.
  3. But if we create a sub-class object ,we can access both super class and sub-class members. So thats why we always create sub-class object in inheritance.
  4. Sometimes the super class members and sub-class members may have same name.
  5. In that case by default sub-class members are accessible. See the following example:

  6. InheritanceExample3.java
    package java8s;

    class A1

        {
           int X=50;
         }
    class B1 extends A1
        {
           int X=100;
           void show()
               {
                   System.out.println("X="+X);
               }
          }
         public class InheritanceExample3
         {
            public static void main(String[] args){
                   B1 ob=new B1();
                   ob.show();
               }
         }

    Output:
    X=100


  7. Here we observed that when ob.show() executes , the sub-class instance variable p value 100 is displayed. Then point is how to access the super class members from sub-class.
  8. So for this purpose the keyword super is invented.
  9. The super keyword refers to the super class member from a sub-class.
  10. The keyword super can be used to access super class variables as super.variable.
  11. The keyword super can be used to access super class method as super.method().
  12. The keyword super can be used to access super class constructor..
  13. To call the parameterized constructor, we write super(values).

Now let’s see that program InheritanceExample3.java to access super class data:


package java8s;

class A1
      {
            int X=50;
      }
class B1 extends A1
      {
            int X=100;
            void show()
           {
               System.out.println("X="+super.X);
               System.out.println("X="+X);
           }
      }
     public class InheritanceExample3 {
           public static void main(String[] args){
               B1 ob=new B1();
               ob.show();
           }
     }

inheritance

To access super class method, see the following example;


InheritanceExample4.java

package java8s;

class A2
      {
            void get()
           {
               System.out.println("JAVA means SILAN Technology");
           }
      }
class B2 extends A2
      {
       void get()
       {
            super.get();
            System.out.println("java8s.com");
       }
     }

     public class InheritanceExample4
           {
           public static void main(String[] args)
           {
               B2 ob=new B2();
               ob.get();
           }
     }

inheritance2

To access super class constructor, see the following example,


InheritanceExample5.java

package java8s;

class A3
      {
            A3()
               {
                  System.out.println("Hii");
               }
      }
class B3 extends A3
      {
            B3()
           {
               supper();
               System.out.println("Hello");
           }
      }
     public class InheritanceExample5
     {
           public static void main(String[] args)
           {
               B3 ob=new B3();
           }
     }

inheritance3

Follow Us


❮ Previous Next ❯



  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved