• Post Reply Bookmark Topic Watch Topic
  • New Topic

Classes - a simple explanation if possible  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I have a good handle on classes and instances of classes but I am looking for answers to some questions please?

Example: you have 3 classes:
1 A point class which has 2 private integer variables x and y and performs all the setter and getter methods for x, y and x and y and methods.

2. A triangle class which has 3 variables of type Points (the first class).

3 A driver class.

The code works and runs fine but my questions are:

1. In the triangle class your constructor is:



could you write it instead as


V1, V2, V3 are private instances of class Points.

Is this not the same and what if any effect would this have on the driver class when calling methods etc?

Thanks in advance

 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Kellytt wrote:1 A point class which has 2 private integer variables x and y and performs all the setter and getter methods for x, y and x and y and methods.

Ah, but that's only one way to think about it. Another is to make it immutable, viz: and then just provide getters for x and y. It's a slightly more functional style, but it tends to make the API a lot smaller.

BTW, x and y are not points, they're simply axis values; and your class describes a single point, so its name should probably be singular too.

2. A triangle class which has 3 variables of type Points (the first class).
...
In the triangle class your constructor is:class triangle(int x1, int y1, int x2, int y2, int x3, int x4){[/code]

And why would you do it like that? Surely, you could just have:
public Triangle(Point p1, Point p2, Point p3) {

Is this not the same and what if any effect would this have on the driver class when calling methods etc?

Looks like it to me, but obviously your driver class would have to call those methods rather than plain old setX() and setY().

You'll discover as you go further that there's almost always more than one way to skin a cat, which is why there's rarely a "right" way to do things.

HIH

Winston
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much.

This is my fault. There were 2 constructor classes with one as you recommended but I didn't tell you in advance.
Yeah, I am sure there are more ways to skin a cat. I haven't even started working with final just yet but it is only when you think you have mastered something that you will start being adventurious and trying out other things.

Anyway thanks again for your time. You answered my question and now I can delve into inheritance and the exercises assosiated with it.

I'm sure now, it will get a little easier
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Kellytt wrote: . . .
could you write it instead as


V1, V2, V3 are private instances of class Points.

Is this not the same . . .
No, you (probably) can't. If you simply write that, you may not have instantiated the three Points, and trying to use non‑existent objects will give rise to an Exception.
Or, put differently, the three references might still point to null.

I had to alter your code tags: one lot of code tags per block please, not one per line. It is much easier to read now.
You might do well to change the constructor to
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I trust you will have seen the error in my last post by now

It was not intentional; I made a typing error in line 03. But having made that error, I decided to perpetuate it rather than correcting it
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you know that you can use the this(); syntax to call one constructor from the other?
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Campbell, thanks for your feedback.

Of course the V1.setXY(x1,y1) and the 2 other Vs are contained in a constructor with the 6 parameters of x and y but appreciate what you are saying.
There were 2 constructor with the 3 Points types as parameters and the V1,V2 and V3 assigned to them using "this".

It is great to get the insight of others particularly as mine is limited right now.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!