The Artima Developer Community
Sponsored Link

Java Answers Forum
constructors

2 replies on 1 page. Most recent reply: May 9, 2003 3:04 AM by Arbab-ul-hassan

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 2 replies on 1 page
dave

Posts: 1
Nickname: javadout
Registered: May, 2003

constructors Posted: May 8, 2003 6:32 AM
Reply to this message Reply
Advertisement
I am working on a problem:

"Write a two and three argument constructor for Circle
a. 2 arguments color and owner
b. 3 arguments color, owner and radius"

I think it should look like this:

public class Circle
{
public Circle
{
String color;
String owner;
}

public Circle
{
String color;
String owner;
String radius;
}
I also need to construct two Circle objects. (does this have to be done here or can I do it in the main() )

The class is inheriting from a Shape class that has private attributes owner and color.

If my code is wrong, please let me know. My main question is why do I need two constructors (why can't I put all attributes in one)? Does it have to do with inheritence?

Any comments much appreciated!


Alex S

Posts: 27
Nickname: andu
Registered: Mar, 2003

Re: constructors Posted: May 9, 2003 12:58 AM
Reply to this message Reply
A constructor is like a method except that it does not return a value. So, yours constructors are messed up. Try to read JavaTutorial to see what a constructor is and how to write one.

Arbab-ul-hassan

Posts: 4
Nickname: umpirepk
Registered: Dec, 2002

Re: constructors Posted: May 9, 2003 3:04 AM
Reply to this message Reply
Firstly I would like to say that The Coding for Writing Constructor is Wrong.

Your Declared Constructor Is:

public Circle
{
String color;
String owner;
}

But This Is Wrong If you Want To Declare This Construcotor
You Should Write This:

public Circle(){
String color;
String owner;
}

And if you want to get Access from Shape Attributes color and owner you should access them via method from shape class.

Flat View: This topic has 2 replies on 1 page
Topic: algorithms Previous Topic   Next Topic Topic: Java MIDlets

Sponsored Links



Google
  Web Artima.com   

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