WAP that illustrates method overriding. Class A3 is extended by Class B3. Each of these classes defines a hello(string s) method that outputs the string “A3: Hello From -” or “B3: Hello From -” respectively. Use the concept Dynamic Method Dispatch and keyword super.
Code:
Output:
Code:
class A3
{
String S;
A3(String s)
{
hello(s);
}
public void hello(String s)
{
S = s;
System.out.println("A3 : Hello From "+S);
}
}
class B3 extends A3
{
B3(String s)
{
super(s);
}
public void hello(String s)
{
S = s;
System.out.println("B3 : Hello From "+S);
}
}
class S5P2_Override
{
public static void main(String args[])
{
A3 a = new A3("DD");
A3 b = new B3("DD");
}
}
Output:
No comments:
Post a Comment