Results 1 to 5 of 5
  1. #1
    spartan is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Inheritence : How the inherited methods behave

    Hi,

    What will be the access modifiers of the inherited methods in subclasses and how they will behave. for example

    class A {

    void method1() {}
    public void method2() {}
    protected void method3() {}
    private void method()4 {}

    }


    class B extends A {

    void someMethod() {}

    }

    Now class B will inherit the methods : method1(default access), method2(public access), method3(protected access). Method4 will not be inherited, since it is a private method.

    So if another class C extends B, the same inheritence rules apply to the methods(inherited from Class A) in class B?
    ie. the access modifiers of the inherited methods in Class B is same as in class A and the same inheritence rules apply?

    Can anyone explain this?

    Thanks,
    Guru
    Last edited by spartan; 05-23-2011 at 07:23 AM.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Java does not support multiple inheritance. However, here is some reading that will interest you...

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by spartan View Post
    class A {
    ...
    }

    class B extends A {
    ...
    }

    So if another class C extends B....
    This seems to be multilevel inheritance and NOT multiple inheritance.

    In this case, the rules will be same for all the below classes who extends the top level class. So in your case, class C will also inherit the 3 methods from class B, which class B has inherited from class A.

    This article might help for more details : Java Tutorial - Multilevel Inheritance

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    B.vijayanand is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default inheritance method

    class A {

    void method1() {}
    public void method2() {}
    protected void method3() {}
    private void method()4 {}

    }


    class B extends A {

    void someMethod() {}

    }


    hi this is your code;

    void method1()-access modifier means you use only package not for any other package...
    public void method2()-public use all classes.
    protected void method3()-you use single package in class...
    private void method4()-you use only one class...

    but you create class C means extends class B its satisfy for all method except private...

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Quote Originally Posted by goldest View Post
    This seems to be multilevel inheritance and NOT multiple inheritance.
    That's what I get for foruming before bed. Thanks... :P

Similar Threads

  1. Inheritence
    By UJJAL DHAR in forum New To Java
    Replies: 0
    Last Post: 12-04-2010, 09:11 AM
  2. Button doesn't behave
    By veryCHEWEY in forum New To Java
    Replies: 6
    Last Post: 02-25-2010, 12:44 PM
  3. [SOLVED] Disabling Inherited Methods
    By Singing Boyo in forum New To Java
    Replies: 7
    Last Post: 05-17-2009, 07:04 AM
  4. Getting around inheritence
    By nick2price in forum New To Java
    Replies: 3
    Last Post: 01-18-2009, 06:48 AM
  5. hiding inherited methods
    By java_fun2007 in forum New To Java
    Replies: 1
    Last Post: 01-05-2008, 02:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •