Results 1 to 10 of 10
  1. #1
    kampong is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default Simple newbie codes..

    Hi
    i taking some course now on OO
    can someone explain these codes to me?
    i cant seems to understand

    class Root {
    public Root() {System.out.println("Root Object");}
    public Root(String s) {System.out.println("Root " + s);}
    public void grow() {System.out.println("Root grows!");}
    }
    class Tree extends Root {
    public Tree() {System.out.println ("Tree Object");}
    public Tree(String s) {System.out.println("Tree " + s);}
    public void grow() {
    super.grow();
    System.out.println("Tree grows!");
    }
    }
    class Leaf extends Tree {
    public Leaf() {System.out.println( "Leaf Object");}
    public Leaf(String s) {System.out.println("Leaf " + s);}
    }

    public class Forest {
    public static void main (String[] args) {
    new Leaf( "FOREST" );
    Root root1 = new Tree(),
    root2 = new Leaf();
    root1.grow();
    root2.grow();
    }
    }


    results:
    Root Object
    Tree Object
    Leaf FOREST
    Root Object
    Tree Object
    Root Object
    Tree Object
    Leaf Object
    Root grows!
    Tree grows!
    Root grows!
    Tree grows!


    but how come??

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

    Default

    which part confuses you?

    One key thing to know is that whenever you call a class's constructor, it first calls the constructor of the parent's class all the way up the the parent tree (as you're seeing).

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

    Default

    To the original poster, cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.

    Keep this up, and you will be placed on the do-not-help list of many of the volunteers here and elsewhere.

  4. #4
    kampong is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    i cant figure out why the order is like tat..
    coz i am quite new to java too..
    which one is the parent class?
    which one is the parent tree like u said?

  5. #5
    kampong is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    oh!
    sorruyy
    really new to here and i dont know the rules well
    :X
    will not repeat cross posting again

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

    Default

    Java Code:
    class Root
    {
    
    }
    Java Code:
    class Tree extends Root
    {
    
    }
    Please take an educated guess: which is the parent class and which is the child class?

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

    Default

    Also, have a look at your text book and here for info on basic OOP concepts: Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials > Learning the Java Language)

  8. #8
    kampong is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Java Code:
    class Root
    {
    
    }
    Java Code:
    class Tree extends Root
    {
    
    }
    Please take an educated guess: which is the parent class and which is the child class?
    Root is the parent class
    which line is "call class's constructor"?

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

    Default

    My example has no constructors but is just to illustrate an inheritance hierarchy. Are you familiar with what a constructor is and how it is called? If not, then please study your text and the link I provided. You've much to learn but with diligent study you'll get there!

  10. #10
    kampong is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    ok. will read up first.
    tks for the link :)
    any doubts will ask u again
    :D

Similar Threads

  1. Need Help With VERY Simple Program -- Newbie!!!
    By somethingfast in forum New To Java
    Replies: 17
    Last Post: 11-25-2008, 08:38 AM
  2. Assistant on my codes. SOS!!
    By sya1912 in forum Java Applets
    Replies: 16
    Last Post: 09-01-2008, 03:23 PM
  3. What's wrong with my codes?
    By ayoood in forum New To Java
    Replies: 16
    Last Post: 09-01-2008, 04:57 AM
  4. Simple newbie problem: import 3rd party jar
    By jodyflorian in forum Eclipse
    Replies: 5
    Last Post: 05-30-2008, 05:02 AM
  5. Posting codes and help
    By Java_Man in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 04:15 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
  •