The Artima Developer Community
Sponsored Link

Java Answers Forum
Inheritance

1 reply on 1 page. Most recent reply: May 31, 2002 10:05 AM by Bart Pelsmaekers

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Guido

Posts: 38
Nickname: kiethb
Registered: May, 2002

Inheritance Posted: May 29, 2002 11:54 AM
Reply to this message Reply
Advertisement
This is the problem statement:

Create 2 classes, Shape and Circle. Have class Circle extend class Shape. Create a static method in class Shape called sayHello() that prints "Nice Form" to the standard output. Declare a method with the same name and signature in class Circle that prints out "Well Rounded" to the standard output. Create a class named Problem1 with a main() method that creates a Circle object and stores its reference in a variable tupe Shape. Invoke sayHello() on the reference to the Circle object.

the code I created from this statement:
public class Shape{
 
	 public static void sayHello(){
 
		System.out.println("nice form");
	}
}
 
public class Circle extends Shape{
 
	 public static void sayHello(){
 
		System.out.println("well rounded");
	}
}
 
public class Problem1{
 
	public static void main(String arg[]) {
 
		Shape shape;
		Circle circle = new Circle();
		shape = circle;
		shape.sayHello();
	}
}


My question is this. does this code satisfy what appears to be asked of me in the statement? Also, given the code, would you expect it to say "nice form" or "well rounded"?

My problem is, I fully expected that the reference to circle (which is 'shape') would retain the overridden method values of sayHello() in Circle. when I run this, the method call still gives the value of the method in the superclass, ("nice form") why is this? does it have something to do with the methods being static? if I should expect the reference to circle via shape to output "well rounded" then what am I doing wrong.

this may actually be the correct answer to the problem I just dont understand what it was getting at.

thanks for any help you can provide.

Kiethb


Bart Pelsmaekers

Posts: 12
Nickname: ikken
Registered: May, 2002

Re: Inheritance Posted: May 31, 2002 10:05 AM
Reply to this message Reply
well, the problem is that you can't redefine static methods! so you just leave out the "static" and this will work.

Methods only have to be static when you call them from a static content (for example: from your main-routine).

Flat View: This topic has 1 reply on 1 page
Topic: syntax error using a method Previous Topic   Next Topic Topic: How to run other Java program in a java application

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use