Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can we call methods in different classes while achieving hierarchical inheritance

I have created total of 14 classes where a1 is my super class and a2,a3,a4,….. are my sub classes. I have initialized int variables in super class and has declared a n9 variable in a2, b1, c1. I want to use a3 subclass to call add5 method which is declared in a5 also I want to use a4 to call add6,add7,add8 methods which are declared in a6,a7,a8 respectively but it’s throwing an error I can’t resolve(see images for reference)

class a1{

    int n1=80,n2=70,n3=60,n4=50,n5=40,n6=30,n7=20,n8=10;
}

class a2 extends a1{

    int n9;
}


class a3 extends a2{

    add5();
}


class a4 extends a2{
    add6();
    add7();
    add8();
}


class a5 extends a3{

    public void add5(){

        n9=n1+n2+n3+n4+n5;
        System.out.println("sum is : "+n9);
    }
}


class a6 extends a4{

    public void add6(){

        n9=n1+n2+n3+n4+n5+n6;
        System.out.println("sum is : "+n9);
    }
}


class a7 extends a4{

    public void add7(){

        n9=n1+n2+n3+n4+n5+n6+n7;
        System.out.println("sum is : "+n9);
    }
}


class a8 extends a4{

    public void add8(){

        n9=n1+n2+n3+n4+n5+n6+n7+n8;
        System.out.println("sum is : "+n9);
    }
}


class b1 extends a1{
    int n9;

}


class b2 extends b1{

    public void sub2(){

        n9=n1-n2-n3;
        System.out.println("subtraction is : "+n9);
    }
}


class b3 extends b1{

    public void sub3(){

        n9=n1-n2-n3-n4;
        System.out.println("subtraction is : "+n9);
    }
}


class c1 extends a1{
    int n9;

}


class c2 extends c1{

    public void mul2(){

        n9=n1*n2*n3;
        System.out.println("multi is : "+n9);
    }
}


class c3 extends c1{

    public void mul3(){

        n9=n1*n2*n3*n4;
        System.out.println("multi is : "+n9);
    }
}

class hei{
    public static void main(String[] args) {
        a8 obj1= new a8();
        obj1.add8();    
    }
}
[the inheritance structure][1]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

First of all you can not declare methods like this: add7(); Java will not allow that. Second, your implementation is terrible. Third, your description is wrong, you say: "I want to use a3 subclass to call add5" a3 is not subclass it is parent class. You can not call child class’s methods from parent class because parent will not have it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading