Results 1 to 3 of 3

Thread: [RESOLVED] inheritance - a beginner's question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Posts
    183

    [RESOLVED] inheritance - a beginner's question

    Ello,

    If class "widget", extends "something", is it possible to create an instance of something then use it it as a widget?

    Here's the code I'm trying to get to work:

    VB Code:
    1. class universe
    2. {
    3.     public static void main(String[] args)
    4.     {
    5.         something whoKnows;
    6.         whoKnows = new widget();
    7.         System.out.println(whoKnows.getWeight());
    8.     }
    9. }
    10.  
    11. class something
    12. {
    13.     public int weight=2;
    14.    
    15. }
    16.  
    17. class widget extends something
    18. {
    19.     public int getWeight()
    20.     {
    21.         return weight;
    22.     }
    23. }

    Output:
    C:\JAVA\INHERI~1>javac universe.java
    universe.java:8: cannot find symbol
    symbol : method getWeight()
    location: class something
    System.out.println((widget)whoKnows.getWeight());
    ^
    1 error
    Thanks!
    Last edited by Evil_Cowgod; Apr 19th, 2006 at 03:38 AM.
    When you ask a question, try and answer a few

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: inheritance - a beginner's question

    Quote Originally Posted by Evil_Cowgod
    If class "widget", extends "something", is it possible to create an instance of something then use it it as a widget?
    No, that would be backward inheritance

    In an inheritance chain, if extending a class is going downwards, classes are "upwards" compatable, but not downwards, if you see what I mean. So if widget extends something you can use a widget instance as a something instance but not the other way round.

    If you override a something method in widget then you may alter the behaviour, but you can't remove a method from a class.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Posts
    183

    Re: inheritance - a beginner's question

    Ok, thanks
    When you ask a question, try and answer a few

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