Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Triple Inheritance

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Triple Inheritance

    I remember that i had read that multi inheritance is not allowed in JAVA.So imagine the hierarchy is like this in my code
    A
    |
    |
    v
    B
    |
    |
    v
    C

    An ugly way to make C inherit what comes from A is to write the vary same functions to function B and to call the functions of A there.Isn't there a more elegant-smart way to do this?

    The variables can not be inheritted in any way?


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Triple Inheritance

    I also thought that we were able to use the same functionality with the interfaces(a class can implement two or more intrefaces if wanted) but i do not think that is works for the class inheritance.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Triple Inheritance

    I don't understand the inheritance structure you wrote, but if you want 'multiple' inheritance - which you cannot directly do in java - use interfaces. Imagine the following: A is an interface, implemented by Aimpl. B is an interface, implemented by the class Bimpl. You wish C to inherit from A and B (actually inherit from Aimpl and Bimpl), then C implements A and B. C can then delegate the the appropriate method calls to instances of Aimpl and Bimpl, and override/extend the behavior it wishes to.

    For instance, a very simple and poor example - forgive the poor naming, lack of commentary, and lack of this actually doing anything interesting:
    public interface A{
        public void doA();
    }
    public class Aimpl implements A{
        @Override
        public void doA(){
            System.out.println("A");
        }
    }
     
    public interface B{
        public void doB();
    }
    public class Bimpl implements B{
        @Override
        public void doB(){
            System.out.println("B");
        }
    }
     
    public class C implements A, B{
        private A ai = new Aimpl();
        private B bi = new Bimpl();
     
        @Override
        public void doA(){
            ai.doA();
        }
        @Override
        public void doB(){
            bi.doB();
        }
    }

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Triple Inheritance

    Your original post suggests you want B to extend A, and C to extend B. And that's fine - ie the following compiles:

    class A {}
    class B extends A {}
    class C extends B {}

    Java doesn't allow classes to inherit the implementation of multiple direct or immediate super classes, but that's all. Animal species, genera etc are the same: but a cat can be-a feline and a mammal.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Triple Inheritance

    Copeg i also thought of what you said but i think is too much.

    pbrocway2 check this example.

    Inheritance.java
    package inheritance;
     
    public class Inheritance {
        public static void main(String[] args) {
            C test=new C();
            test.foo();
            test.bla();
        }
    }

    A.java
    package inheritance;
     
    public class A {
        private int year;
        public  int getYear(){
            return year;
        }
    }

    B.java
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package inheritance;
     
    /**
     *
     * @author User
     */
    public class B extends A {
        private int day;
     
        public final int getDay(){
            return day;
        }
     
        public void foo(){
            int year=super.getYear();
            day=15;
        }
     
    }

    C.java
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package inheritance;
     
    /**
     *
     * @author User
     */
    public class C extends B {
        public void bla(){
            int day=super.getDay();
            int year=super.getYear();
        }
    }

    this works good but in a larger program i have now i can not achieve this.Something else might be the problem.I am going to look it up
    thanks

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Triple Inheritance

    You don't need those "super" keywords.

    Instances of C are also instances of B and they are also instances of A. So it is perfectly acceptable (and clearer) to write

    package inheritance;
     
    public class C extends B {
        public void bla(){
            int day = getDay();
            int year = getYear();
        }
    }

  7. #7
    Junior Member psabbate's Avatar
    Join Date
    Aug 2012
    Posts
    20
    My Mood
    Amused
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: Triple Inheritance

    You can inherit attributes but should be careful regarding their access modifier. Only protected attributes can be accessed directly by sub classes.
    Everyone wants to go to heaven ... but nobody wants to die

    Nissi Group, Servicios IT,
    Diseño Web, Desarrollo de Software, SEO,
    LinkedIn

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Triple Inheritance

    Quote Originally Posted by psabbate View Post
    Only protected attributes can be accessed directly by sub classes.
    Perhaps you mean only protected and public Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  9. #9
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Triple Inheritance

    There are some options, ranging from elegant to down right ugly, depending on the need for this "double inheritance" (I believe it's not triple). Without a hard example, I don't think I can really figure out the best way to achieve the desired effect.

Similar Threads

  1. hi need help with inheritance
    By fredsilvester93 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 19th, 2012, 04:01 PM
  2. Inheritance not working!
    By u-will-neva-no in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 20th, 2012, 06:59 PM
  3. Inheritance
    By lewzax in forum Object Oriented Programming
    Replies: 4
    Last Post: July 8th, 2011, 01:51 PM
  4. inheritance help
    By justin3492 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 07:45 PM
  5. inheritance
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2010, 09:23 PM