Overriding

 package com.company;

//overloading means one class have more than one methods have same name but different argument/parameter
class A{
public void met2() {
System.out.println("I am a method 2 of class A");//met2 o/p of classA
}
public int kaps(){
return 10;
}
}
class B extends A{
@Override //it means we have used override and easy to understand we have used override
public void met2() { /*method overriding means the
same name of methods of child class implements & parent class can run
with same name but give different output and implementation */
//met2 o/p of classB
System.out.println("I am a method 2 of class B");
}
public void met3(){
System.out.println("I am a method 3 of class B");
}

}
public class Method_overriding {
public static void main(String[] args) {
A a=new A();
// System.out.println( a.kaps());
a.met2();
B b=new B();
b.met2();
}
}


o/p
I am a method 2 of class A
I am a method 2 of class B

Comments

Popular posts from this blog

Inheritence

Number_guessing_game

Dynamic_method_dipatch