Remove override Solution – 2: super keyword

Super keyword is second solution of remove override from java programs. Using of super keyword we can call super class methods and access super class variables.

 

 

class A

{

                void method()

                {

                                System.out.println(“Welcome in A Class”);

                }

}

class B extends A

{

                void method()   // Override

                {

                                // Need Super keyword

                                //super.method();          // call to super class method()

                                System.out.println(“Welcome in B Class”);

 

                                super.method();              // call to super class method()

                }

}

class inh6

{

                public static void main(String args[])

                {

                                B b1 = new B();

                                b1.method();     // Call to B Class and override

                }

}

 

Output:

Welcome in A

Welcome in B

 

About Programs.

In this program, both class A and B have same function method() with same parameter. When we call method() from main class, that time method of B class executed. From Sub class B, we use super.method(), this statement call to super class method().

 

Video available on YouTube.


Earn Money Online

464total visits,2visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Comments are closed.