• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi please go through and find a solution for this atleast in a different way.

You are given a class hierarchy with an instance of the class Dog. The class Dog is a child of mammal and the class Mammal is a child of the class Vertebrate. The class Vertebrate has a method called move which prints out the string "move". The class mammal overrides this method and prints out the string "walks". The class Dog overrides this method and prints out the string "walks on paws". Given an instance of the class Dog,. how can you access the ancestor method move in Vertebrate so it prints out the string "move";
 
sethu chiyan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


class Vertibrate
{
public void move()
{
System.out.println("Move");
}

}

class Mammal extends Vertibrate
{
public void move()
{
super.move();
System.out.println("Walks");
}
}

class Dog extends Mammal
{
public void move()
{
super.move();
System.out.println("Walks on paws");
}
}

public class Mobile
{
public static void main(String[] args)

{
Dog _dog = new Dog();
_dog.move();
}
}

I have tried in this way, the o/p is:
Move
Walks
Walks on paws

Anyway I know this is not the exact answer what you expect,but i know we cannot use super.super.move();

Still am waiting for Gi's response.. pl guide me 2.

with regards,
Chiyan
 
Kalyani Marathe
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought this way. Check the code. I just made method static and assigned Vertebrate ref an instance of Dog.
class Vertibrate
{
public static void move()
{
System.out.println("Move");
}

}

class Mammal extends Vertibrate
{
public static void move()
{
//super.move();
System.out.println("Walks");
}
}

class Dog extends Mammal
{
public static void move()
{
//super.move();
System.out.println("Walks on paws");
}
}

public class Mobile
{
public static void main(String[] args)

{
Vertibrate ver = new Dog();
ver.move();
}
}
 
Tejal Modi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kal,
u r xactly correct.
with regards,
Chiyan.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic