4. Consider the following classes: public class Animal { public Animal(String itsName) { name public void setName (String itsName) { name public String getNane() { return name; } } } private String name; } public class Dog extends Animal public Dog(String itsName) { super(itsNane + "Dog"); sound = "Bark!"; { } public void şsetsaund(String itesound) { sound = public String getSound (0 { return (getName (0) + sound); public String getName (int choice) { if(choice > 0) return sypergetlans (); else return "AnxDea"; } public String getNane() { return surSKgetNans () + "OK"; } private String sound; } In addition, the following lines of code has been placed in a main class public static void main (String[] args) Animal al = new Animal("Tweety"); Animal a2 = new Dog ("Rover"); Dog d1 = new Dog("Spot"); } { Give the values of the following expressions. If an expression is erroneous, state why. a) al.getName() e) dl.getName() f) dl.getName(1) g) ((Animal) d1).getNane() h) ((Dog)al).getNane() i) d1 instanceef Animal j) al instanEsef Dog k) al.getSound () 1) a2.getSound () m) dl.getSound () n) ((Dog)a2).getseund () ( (Animal) dl).getsewnd() b) a2.getName() c) a2.getName (0) invalid because the function has no parameter but the parameter 0 is provided here. So this is invalid. d) (new Animal ()).getNane () o) 5. Which of the following best illustrates the concept of polymorphism? Why? i) That a) and b) have different answers. ii) That e) and f) have different answers. iii) That k) and 1) have different answers.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

JAVA

 

I need help on question 5, Please and thank you so much!

4. Consider the following classes:
public class Animal
public Animal(String itslane) { name
public void setlane (String itslame) { name
{
}
}
public String getName() { return name;
}
private String name;
}
public class Dog extends Animal
public Dog (String itsAne)
{ super (itsNane + "Dog");
sound = "Bark!";
{
}
public void şatsevnd (String itaseund) { sound =
public String getsound () { return (gatNane (0) + sound);
public String getName (int choice)
{ if(choice > 0) return supergetNane () ;
else return "AnyDag" ; }
public String getName () { return super get Name () + "OK"; }
}
}
private String sound;
}
In addition, the following lines of code has been placed in a main class
public static void main (String[] arge)
{
Animal al = new Animal ("Tweety");
Animal a2 = new Dog ("Rover");
Dog d1 = new Dog ("Spot");
}
Give the values of the following expressions. If an expression is erroneous, state why.
a) al.getName()
e) d1.getName ()
f) dl.getName(1)
g) ( (Animal) d1).getNane()
h) ((Dog)al).getNane,()
i) d1 instanEef Animal
j) al instanceef Dog
k) al.getSound ()
b) a2.getName()
c) a2.getName(0)
invalid because the
function
has no parameter but the
1) a2.getSound ()
m) dl.getSound ()
n) ((Dog)a2).getseund()
o) ((Animal) d1).getsownd ()
parameter 0 is provided
here. So this is invalid.
d)
(new Animal () ).getNane()
5. Which of the following best illustrates the concept of polymorphism? Why?
i)
That a) and b) have different answers.
ii)
That e) and f) have different answers.
iii)
That k) and 1) have different answers.
Transcribed Image Text:4. Consider the following classes: public class Animal public Animal(String itslane) { name public void setlane (String itslame) { name { } } public String getName() { return name; } private String name; } public class Dog extends Animal public Dog (String itsAne) { super (itsNane + "Dog"); sound = "Bark!"; { } public void şatsevnd (String itaseund) { sound = public String getsound () { return (gatNane (0) + sound); public String getName (int choice) { if(choice > 0) return supergetNane () ; else return "AnyDag" ; } public String getName () { return super get Name () + "OK"; } } } private String sound; } In addition, the following lines of code has been placed in a main class public static void main (String[] arge) { Animal al = new Animal ("Tweety"); Animal a2 = new Dog ("Rover"); Dog d1 = new Dog ("Spot"); } Give the values of the following expressions. If an expression is erroneous, state why. a) al.getName() e) d1.getName () f) dl.getName(1) g) ( (Animal) d1).getNane() h) ((Dog)al).getNane,() i) d1 instanEef Animal j) al instanceef Dog k) al.getSound () b) a2.getName() c) a2.getName(0) invalid because the function has no parameter but the 1) a2.getSound () m) dl.getSound () n) ((Dog)a2).getseund() o) ((Animal) d1).getsownd () parameter 0 is provided here. So this is invalid. d) (new Animal () ).getNane() 5. Which of the following best illustrates the concept of polymorphism? Why? i) That a) and b) have different answers. ii) That e) and f) have different answers. iii) That k) and 1) have different answers.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY