Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Single program should contain encapsulation,Polymorphism, inheritance

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Gangadhararao Bommasani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you so much.. I will make the changes as your mentioned way.
 
Greenhorn
Posts: 1
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//This java pgm contains abstraction,polymorphism,encapsulation and inheritance

class Parent_1
{
private int i;//encap
private int j;

public void display() {

System.out.println("these are the 2 answer");
}
}
class child extends Parent_1 //inher
{
public void display()//method overiding
{
System.out.println("this is for method overriding");
}


public void mul(int i, int j)
{
int k=i*j;
System.out.println("mul of 2 int val is:"+k);

}
public void mul(double i,double j)//method overloading
{
double z=i*j;
System.out.println("mul val of 2 double is:"+z);
}
}
class Son
{

public static void main(String args[])
{
Parent_1 p=new Parent_1();

Parent_1 pt=new child();
child cd=new child();
p.display();
cd.mul(2, 20);
cd.mul(2.2, 1.1);
pt.display();
}
}





run:
these are the 2 answer
mul of 2 int val is:40
mul val of 2 double is:2.4200000000000004
this is for method overriding
BUILD SUCCESSFUL (total time: 2 seconds)
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gangadhararao Bommasani wrote:
i have modified the class.. Could you please check it out whether below class supports encapsulation,polymorphism, inheritance concepts?


This is very general question.... It doesn't show whether you understand these concepts or not.

Gangadhararao Bommasani wrote:
private int a;


Why you define it private? What if we define it public? what will be the difference?

Gangadhararao Bommasani wrote:
class Boo extends Too


What this statement will do?

Gangadhararao Bommasani wrote:public void setMethod(int a)
{
this.a = a;
}


change the method in Too class to setMethodA, run your program and try to see what is happening

We are waiting for your queries
 
reply
    Bookmark Topic Watch Topic
  • New Topic