Override methods in java program

Override methods V/S Overloading methods

Override methods is dynamic polymorphism. Overloading methods is static polymorphism.

Name of methods and parameter of methods are same in override.

Name of methods are same but parameter of methods are not same in overloading method.

 

 

“Override method possible only in inheritance class, because we can’t create same methods in same class.”

In this override method process sub class’s method overrides super class’s method. So super class method does not work on output screen.

 

class A

{

                void method()

                {

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

                }

}             

class B extends A

{

                void method()   // NAme == method and Parameter () == same

                {

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

                }

}

class inh4

{

                public static void main(String args[])

                {

                                B obj = new B();

                                obj.method();   // override B class method to A Class method

                }

}

 

Output:

Welcome in B

 

About Programs.

In this program, Super class A has method() function and Sub class B has same method() function and both class in inheritance. So when will call method() function that time sub class B’s method() function will override super class A’s method() function.

 

Video available on YouTube.

 


Earn Money Online

626total visits,5visits today


mayank

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

Related articles

One response to “Override methods in java program”