java program to add three numbers in java using inheritance..please help

class A
{
int a;
int b;
}
class B extends A
{
int c;
}
class C extends B
{
int d;
public int sum()
{
d=a+b+c;
return d;
}
}
class add
{
public static void main(String args[])
{
A obj1= new A();
B obj2= new B();
C obj3= new C();
obj1.a = 1;
obj1.b = 2;
obj2.c = 3;
System.out.println("sum is"+obj3.sum());
}
}
the output it showed was sum is 0
then i wrote again the code
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;
obj1.showab();
obj2.c = 3;
obj2.showc();
obj3.sum();
System.out.println("sum is"+obj3.sum());
}
}
output showed was 3,3 sum is 0
can anyone plz tell what mistake i made in my two programs and what basic i m lacking that garbage value i,e 0 comes again n again what methodology should i adopt to overcome this.

Replies

  • Anoop Kumar
    Anoop Kumar
    package sample;
    class A {
    int a;
    int b;
    }
    class B extends A {
    int c;
    }
    class C extends B {
    int d;
    public int sum() {
    d = a + b + c;
    return d;
    }
    }
    public class add {​
    public static void main(String args[]) {​
    A obj1 = new A();​
    B obj2 = new B();​
    C obj3 = new C();​
    obj3.a = 1; // you ware creating object from obj1 and obj2 and then​
    obj3.b = 2; //putting values in these and then calling obj3 😲​
    obj3.c = 3;​
    System.out.println("sum is" + obj3.sum());​
    }​
    }​


    and value 0 is not garbage value its default value of class member of Integer type that is 0.​
  • zion
    zion
    thanx......
  • zion
    zion
    what is use or need to create object for A and B..we can directly create for C
  • Anoop Kumar
    Anoop Kumar
    zion
    what is use or need to create object for A and B..we can directly create for C
    You got that.. there is no use now... 😀
  • zion
    zion
    thanx😀
  • oyetayo micheal
    oyetayo micheal

    why it is that multiple inheritance is not allowed in java except when dealing with interface

You are reading an archived discussion.

Related Posts

Hello I'm a marine engineer. CrazyEngineers.com seems to be the most informative site for engineers. I'm happy to be a part of this community.
Nokia brings SmartStart service for its new Windows Phone users to make it super-duper easy for them to get started with a little one-on-one help from experts. And it is...
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...
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...