java program related to inheritance so it shows three numbers and display the sum

class A
{
int a;
int b;
void showab()
{
System.out.println(a+b);

}
}
class B extends A
{
int c;
void showc()
{
System.out.println(c);
}
}
class C extends B
{
int d;
public int sum()
{
d=a+b+c;
return d;
}
}
class add1
{
public static void main(String args[])
{
A obj1= new A();
B obj2= new B();
C obj3= new C();
obj1.a = 1;
obj1.b = 2;
obj3.a = 1;
obj3.b = 2;
obj1.showab();
obj2.c = 3;
obj3.c = 3;
obj2.showc();
obj3.sum();
System.out.println("sum is"+obj3.sum());
}
the output shows 3,3 and sum is 6
i want that output shows 1,2,3 and sum is 6 were my code is wrong

Replies

  • Sandip@1983
    Sandip@1983
    Here you go,

    class A
    {
    int a;
    int b;
    void showab()
    {
    System.out.print(a+","+b);

    }
    }
    class B extends A
    {
    int c;
    void showc()
    {
    System.out.println(","+c);
    }
    }
    class C extends B
    {
    int d;
    public int sum()
    {
    d=a+b+c;
    return d;
    }
    }
    class add1
    {
    public static void main(String args[])
    {
    A obj1= new A();
    B obj2= new B();
    C obj3= new C();
    obj1.a = 1;
    obj1.b = 2;
    obj3.a = 1;
    obj3.b = 2;
    obj1.showab();
    obj2.c = 3;
    obj3.c = 3;
    obj2.showc();
    obj3.sum();
    System.out.println("sum is "+obj3.sum());
    }}
  • zion
    zion
    thanx...😀

You are reading an archived discussion.

Related Posts

I hope you guys had a chance to look at the Google Glass Walk photos - https://www.crazyengineers.com/googl...deo-about-googles-project-glass-surface-2167/ At Zeitgeist 2012, Google's own co-founder Larry Page gave a 20-minute talk that...
If you feel spendy, we've a nice recommendation for you - Blackberry Porsche Design P'9881 is now available in India for only Rs. 130,000. The price is peanuts because it...
I used to like the days when Flipkart was only limited to Books, Mobiles and Computers. But it looks like Flipkart understands the need of venturing into other categories as...
Nice to meet you all. My name is Adzmeel Akmar bun Hj Ayob. Just call me Adzmeel. I'm from Malaysia and study diploma in mechanical engineering. Right now I'm in...
Apparently more women than men receive PhDs in biological Sciences, at least in the US. The story is perhaps true in India too. Another survey shows that women comprise 73%...