class A{
m2(){
}
}
class B extends A{
m2(){
}
}
class c extends B{
m2(){
}
}
class my_class extends c{
m2(){
}
pulic static void main(){
...My_class a = new my_class();
super.super.super.m2(); is this is leagal
if not find what is the legal procedure in order to call A's
version of m2();
}
Answers were Sorted based on User's Feedback
Answer / ranganathkini
No it is illegal to call:
super.super.super.m2();
If the implementation of m2() defined by class A has to be
called from within my_class's implementation of m2(), the
following change must can be made:
class A {
public void m2() {
// call the protected implementation
m2Impl();
}
// a protected implementation of A's m2() method
// giving the implementation a protected access
// allows only subclasses to see the method
// and remains inaccessible to the rest of the world
protected void m2Impl() {
System.out.println( "A.m2() invoked" );
}
}
class B extends A {
public void m2() {
System.out.println( "B.m2() invoked" );
}
}
class C extends B {
public void m2() {
System.out.println( "C.m2() invoked" );
}
}
class my_class extends C {
public void m2() {
// call A's protected implementation
m2Impl();
}
}
public class TestSuperSuper {
public static void main( String[] args ) {
my_class mc = new my_class();
mc.m2();
}
}
Hope it helps! :-)
Is This Answer Correct ? | 10 Yes | 2 No |
Answer / santosh subrahmanya
This will work......
class A{
public void m2(){
System.out.println("called...");
}
}
class B extends A{
public void m2(){
super.m2();
}
}
class c extends B{
public void m2(){
super.m2();
}
}
class my_class extends c{
public void m2(){
super.m2();
}
public static void main(String[] args){
my_class a = new my_class();
a.m2();
}
}
Is This Answer Correct ? | 10 Yes | 2 No |
Answer / sree
class A{
void m2(){
System.out.println("in class A"); }
}
class B extends A{
void m2(){
super.m2();
System.out.println("in class B");
}
}
class c extends B{
void m2(){super.m2();
System.out.println("in class c");
}
}
class Check extends c{
void m2(){
super.m2();
System.out.println("in check()"); }
public static void main(String[] args){
c obj =new Check();
obj.m2();
}
}
Is This Answer Correct ? | 4 Yes | 0 No |
Answer / n. bala subramanian
By reflection we can achive it, I hope this is correct
class A{
void m2(){System.out.println("in class A");}
}
class B extends A{
void m2(){System.out.println("in class B");}
}
class C extends B{
void m2(){ System.out.println("in class c");}
}
public class Test extends C {
void m2(){System.out.println("in class A"); }
public static void main(String[] args) throws Exception{
Class c = Class.forName("com.samples.test.Test");
A obj = (A) c.getSuperclass().getSuperclass
().getSuperclass().newInstance();
}
}
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / debapriya maity
First super keyword cannot be used from a stati
ccontext,this is the first thing to be remembered.The
correct procedure is
class A{
m2(){
super();
}
}
class B extends A{
m2(){
super();
}
}
class c extends B{
m2(){
super();
}
}
class my_class extends c{
m2(){
super();
}
pulic static void main(){
my_class a = new my_class();
a.m2():
}
}
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / kalpit
super.super.super.m2(); is illegal there can be only super.m2();
There is no way that A's m2 & B's m2 can be called using
object of my_class.
A obj =new A();
obj.m2();
is correct
Is This Answer Correct ? | 0 Yes | 1 No |
Answer / monika
Above solution looks correct but it is not taking advantage
of inheritance.
I hope following code does the trick.
class A
{
void m2()
{System.out.println("in class A");
}
}
class B extends A
{
void m2()
{ System.out.println("in class B");
}
}
class c extends B
{
void m2()
{
System.out.println("in class c");
}
}
class Check extends c
{
void m2()
{
System.out.println("in check()");
}
public static void main(String[] args)
{
A obj =new Check();
obj.m2();
}
}
Is This Answer Correct ? | 2 Yes | 4 No |
Answer / priynajan
No it is not leagal the correct procedure is:
class A{
void m2(){System.out.println("in class A"); }
}
class B extends A{
void m2(){
System.out.println("in class B");
}
}
class c extends B{
void m2(){
System.out.println("in class c");
}
}
class Check extends c{
void m2(){System.out.println("in check()"); }
public static void main(String[] args){
A obj =new A();
obj.m2();
}
}
..
Works correctly :-)
Is This Answer Correct ? | 1 Yes | 8 No |
What are keywords and reserved words in java?
When will you define a method as static in Java?
0 Answers SwanSoft Technologies,
What are the different ways to handle exceptions?
what is object slice?
What is static and a non-static inner class?
What is use of inner class in java?
What is the use of isempty in java?
Explain wait() method of object class ?
What are the three types of design patterns?
what is struts-config-xml?and its use?
explain about method overloading and method overriding with difficult examples
What are inner and anonymous class?