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

    zion

    Member

    Updated: Oct 25, 2024
    Views: 2.0K
    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.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Anoop Kumar

    MemberMay 27, 2012

    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.​
    Are you sure? This action cannot be undone.
    Cancel
  • zion

    MemberMay 27, 2012

    thanx......
    Are you sure? This action cannot be undone.
    Cancel
  • zion

    MemberMay 27, 2012

    what is use or need to create object for A and B..we can directly create for C
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberMay 27, 2012

    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... 😀
    Are you sure? This action cannot be undone.
    Cancel
  • zion

    MemberMay 28, 2012

    thanx😀
    Are you sure? This action cannot be undone.
    Cancel
  • oyetayo micheal

    MemberFeb 26, 2019

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

    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register