How to call the m1() method of Base class in below snippet ?
class Base
{
public void m1()
{
System.out.println("Base m1 ");
}
public void m2()
{
System.out.println("Base m1 ");
}
}
======================
class Derived extends Base
{
public void m1()
{
System.out.println("Derived m1");
}
public void m3()
{
System.out.println("Derived m3");
}
public static void main(String[] args)
{
Base ob=new Derived();
ob.m1();
//System.out.println("Hello
World!"+ob.m1());
}
}
Answers were Sorted based on User's Feedback
Answer / mitu
class Derived extends Base
{
public void m1()
{
System.out.println("Derived m1");
}
public void m3()
{
System.out.println("Derived m3");
}
public void m26()
{
super.m1();
}
public static void main(String[] args)
{
Derived ob=new Derived();
ob.m1();
ob.m26();
}
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / kishore
Nand asking in the about snippet is saying like: The base class is referenced to derived class object (i.e Polymorphism). As a polymorphism point of view only overidden methods of Derived class will be called. But how is it possible to access the m1() of Base class using the reference of Base Class ?
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / surendra
in main() create an object to derived class and call m1() by using this obj since all super class members are available to
subclass object.
Derived d=new Derived();
d.m1();
| Is This Answer Correct ? | 1 Yes | 2 No |
What are the interfaces in java collections? : java collections
i need java 1+ experience resume on java developer kindly send me my mail id:srikanthreddy1.marla@gmail.com
what is jndi?
when exactly should we use throws ,and wen exactly should we use the try and catch for exceptional handling
What are the considerations to be made in case of loops in java ?
Difference b/w >> and >>> ?
what is java virtual machine
What is java collection? : java collections
What is an Ioc pattern? There are assemblies in .net for establishing this task, Whats the special in Java /j2ee technologies about IOC
In HashSet duplicates are allowed while adding to the HashSet, but while retreiving the object from HashSet is not shown the duplicate values, WHY ?
whatis arguments &what is default argument
What is an algorithm in java collection framework? : java collections