Results 1 to 6 of 6
  1. #1
    v6g
    v6g is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default Embarrisingly simple problem from "a friend"

    ok - so I'm getting back into java after a few years on other things. I'm totally stuck on an inheritance problem - been working on it for days but can't figure a way around it.

    I'm trying to access variables in one subclass from within another subclass.

    My best attempt below - broken down into it's simplest form:
    Java Code:
    public class Test
    {
       public static void main( String[] args )
       {
          Test test = new Test();
       }
    
       public Test()
       {
          Son son = new Son();
          Daughter daughter = new Daughter();
       }
    }
    
    class Son
    {
       String name = "Peter";
       
       public Son()
       {
          System.out.println("My sisters name is: " + [COLOR="Red"]daughter.name[/color]);
       }
    }
    
    class Daughter
    {
       String name = "Jane";
       
       public Daughter()
       {
          System.out.println("My brothers name is: " + [color="RED"]son.name[/color]);
       }
    }
    Any suggestions?
    Last edited by v6g; 11-25-2009 at 11:54 PM.

  2. #2
    tejens23 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    Try this:

    Java Code:
    public class Test extends Son
    You also might need to use the keyword "super" to use variables from the parent class.

    Let me know if this helps.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code has nothing to do with inheritance as you have no super and no child class. Your best bet: crack open a book and start reading from the beginning as that will likely get you up and running faster than random strolls through Java-land.

    Best of luck!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by tejens23 View Post
    Try this:

    Java Code:
    public class Test extends Son
    You also might need to use the keyword "super" to use variables from the parent class.

    Let me know if this helps.
    This will not help. Sorry.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here's an example of inheritance

    Java Code:
    public class Test
    {
       public static void main( String[] args )
       {
          Test test = new Test();
       }
    
       public Test()
       {
          Child son = new Child("Peter");
          Child daughter = new Child("Jane");
          
          son.setSibling(daughter);
          daughter.setSibling(son);
          
          son.doSomething();
          daughter.doSomething();
       }
    }
    
    abstract class Parent {
       private String name;
       
       public Parent(String name) {
          this.name = name;
       }
       
       public abstract void doSomething();
       
       public String toString() {
          return name;
       }
    }
    
    
    class Child extends Parent
    {
       Child sibling;
       
       Child(String name) {
          super(name);
       }
       
       public void setSibling(Child sibling) {
          this.sibling = sibling;
       }
       
       public void doSomething() {
          System.out.println("My name is " + this + " and my sibling is " + sibling);
       }
       
    }

  6. #6
    v6g
    v6g is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    I think that gets me back on the road - thank you gentleman!!

Similar Threads

  1. Simple "if" statement problem....compiling error.
    By CYANiDE in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 10:56 PM
  2. Convert " ü " into simple " u "
    By nitinb4u in forum New To Java
    Replies: 4
    Last Post: 02-23-2009, 09:35 AM
  3. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. Need help with "a simple order entry program"
    By sentica in forum New To Java
    Replies: 6
    Last Post: 10-17-2008, 06:38 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
  •