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

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

342total visits,2visits today


mayank

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

Related articles

1 Comment

  1. October 14, 2016 - 6:49 pm

    all the time i used to read smaller articles which as well clear their motive,
    and that is also happening with this article which I
    am reading at this time.

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image

EmailEmail
PrintPrint