| Author |
Single program should contain encapsulation,Polymorphism, inheritance
|
Gangadhararao Bommasani
Greenhorn
Joined: Dec 28, 2009
Posts: 11
|
|
The following class having polymorphism and inheritance ..
package com.study.over;
class Animal2
{
public void eat()
{
System.out.println("Generic Animal Eating Generically");
}
}
class Horse2 extends Animal2
{
public void eat()
{
System.out.println("Horse eating hay ");
}
public void eat(String s) {
System.out.println("Horse eating " + s);
}
}
public class OopsTest
{
public static void main(String[] args)
{
Animal2 a = new Animal2();
a.eat();
Horse2 h = new Horse2();
h.eat();
Animal2 ah = new Horse2();
ah.eat();
}
}
can anyone add encapsulation to this class? thanks
|
 |
W. Joe Smith
Ranch Hand
Joined: Feb 10, 2009
Posts: 710
|
|
Please place code in code tags, as it makes it much easier to read.
Are you having trouble figuring out how to use encapsulation with this code? We won't write the code for you, but we will do everything we can to help you write it. So, where are you having an issue with encapsulation?
|
SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
|
 |
Gangadhararao Bommasani
Greenhorn
Joined: Dec 28, 2009
Posts: 11
|
|
Thank you for your reply.
i have modified the class.. Could you please check it out whether below class supports encapsulation,polymorphism, inheritance concepts?
class Too
{
private int a;
public void setMethod(int a)
{
System.out.println("a value"+a);
}
}
class Boo extends Too
{
private int b;
public void setMethod(int b)
{
System.out.println("b value"+b);
}
public void setMethod(String s) {
System.out.println("String value " + s);
}
}
public class OopsTest
{
public static void main(String[] args)
{
Too a = new Too();
a.setMethod(4);
Boo h = new Boo();
h.setMethod(5);
Too ah = new Boo();
ah.setMethod(4);
h.setMethod("OOps");
}
}
Thanks
|
 |
W. Joe Smith
Ranch Hand
Joined: Feb 10, 2009
Posts: 710
|
|
In your set methods, you should actually set the value of the variable. Just passing in a parameter won't actually set the private variable. I would also recommend changing your parameter so that its variable name isn't the same as the private variable you are trying to set (personal preference, just makes it easier to see which variable you are referring to without worrying about variable shadowing). So your set methods should look something like this:
Also, for each setMethod you should have a corresponding getMethod to retrieve the value of the variable.
|
 |
Gangadhararao Bommasani
Greenhorn
Joined: Dec 28, 2009
Posts: 11
|
|
Thank you so much.. I will make the changes as your mentioned way.
|
 |
 |
|
|
subject: Single program should contain encapsulation,Polymorphism, inheritance
|
|
|