CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Inheritance

  1. #1
    Join Date
    Sep 2009
    Posts
    20

    Unhappy Inheritance

    it may seem to be a little bit foolish,,,and i m quiet known about inheritance basics that a superclass can't access its sublass members,,,
    but what if i declare any member of a subclass as public even then im not able to access them,,,,and according to public definition it can be called from any code even from outside...
    so do the "inheritance" and "public" concepts contradict each other?????

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Inheritance

    It's not really clear exactly what you're asking. If you post some example code that you have trouble understanding, maybe we can help you understand it.

    It is easier to measure something than to understand what you have measured...
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Sep 2009
    Posts
    1

    Re: Inheritance

    Ehh you're kind of mixing two different concepts.

    Think of inheritance as you would in real life. Children inherit from their parents, and the children's parents inherit from their parents. Parents don't inherit from their children and grandparents don't inherit from their grandchildren.

    You are right about public functions, and them being able to be accessed from anywhere including outside of the code. But when you extend class B to class A (ie: class B extends A) you are letting B become the child and A the parent.

    Then, your object B inherits all of the functions from class A. Class A has no idea that it has children (must be nice), and since it doesn't know about it's children, then it can't really access them.

    However, if you had a static function in B, then you could call it from A. Having a static function applies to the class though and an object. Example:


    Code:
    class A{
        public A(){
            B.display();
        }
    }
    class B extends A{
        public B(){
            super();
        }
        public static void display(){
            System.out.println("Calling class B");
        }
    }
    
    public class inheritance{
        public static void main(String[] args){
            A test = new B();
        }
    }
    Hopefully this [sort of] helps!

  4. #4
    Join Date
    Sep 2009
    Posts
    20

    Re: Inheritance

    in your reply ,i don't think that there is any need to create object of subcls,,,and why hav u used super() here??????

  5. #5
    Join Date
    Apr 2007
    Posts
    425

    Re: Inheritance

    You always have
    Code:
    super();
    there, it was just explicitly typed.

    Think about it though: there are many times where you want to know or use the child object, and not the parent type.

    Let's think about a heirarchy such as a house, where you have a device class, and subclasses of thermostat, refrigerator, oven, hot water tank, lightswitch.

    Can you really control all of them the same way? Well, all of them certainly have an on and off function, and a few other things they have in common, such as how much power they are drawing for example, etc, but there is a lot to be gained from creating objects and using subclass objects rather than just dealing with them all in terms of a device where you only know on/off but maybe you don't know how to increase temperature +2 degrees.

    Is that helping you understand this a bit more?
    ------
    If you are satisfied with the responses, add to the user's rep!

  6. #6
    Join Date
    Sep 2009
    Posts
    20

    Re: Inheritance

    k fine thanxxx

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: Inheritance

    Quote Originally Posted by arpit2309 View Post
    it may seem to be a little bit foolish,,,and i m quiet known about inheritance basics that a superclass can't access its sublass members,,,
    but what if i declare any member of a subclass as public even then im not able to access them,,,,and according to public definition it can be called from any code even from outside...
    so do the "inheritance" and "public" concepts contradict each other?????
    A superclass can in fact access subclass members. This is because a superclass can force a subclass to implement a member by declaring it abstract, like
    Code:
    public abstract class Super {
       public void m1() {
          m2(); // calls method that must be implemented in subclass
       }
       public abstract void m2(); // abstract so it must be implemented by any subclass 
    }
    //
    public class Sub extends Super {
       public void m2() { // must be implemented here because abstract in superclass
       }
    }
    Last edited by nuzzle; September 19th, 2009 at 10:34 AM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured