Results 1 to 10 of 10
Thread: Simple newbie codes..
- 03-11-2009, 01:26 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
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??
-
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).
-
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.
- 03-11-2009, 01:40 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
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?
- 03-11-2009, 01:43 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
oh!
sorruyy
really new to here and i dont know the rules well
:X
will not repeat cross posting again
-
Java Code:class Root { }Please take an educated guess: which is the parent class and which is the child class?Java Code:class Tree extends Root { }
-
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)
- 03-11-2009, 01:58 PM #8
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
-
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!
- 03-11-2009, 02:10 PM #10
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Need Help With VERY Simple Program -- Newbie!!!
By somethingfast in forum New To JavaReplies: 17Last Post: 11-25-2008, 07:38 AM -
Assistant on my codes. SOS!!
By sya1912 in forum Java AppletsReplies: 16Last Post: 09-01-2008, 02:23 PM -
What's wrong with my codes?
By ayoood in forum New To JavaReplies: 16Last Post: 09-01-2008, 03:57 AM -
Simple newbie problem: import 3rd party jar
By jodyflorian in forum EclipseReplies: 5Last Post: 05-30-2008, 04:02 AM -
Posting codes and help
By Java_Man in forum New To JavaReplies: 2Last Post: 02-16-2008, 03:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks