• Post Reply Bookmark Topic Watch Topic
  • New Topic

constructor, class, and method confusion  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm going to try to word this in a way that makes sense, especially since I think understanding it will open up a whole new java world for me. As I'm going through
the Oracle Tutorials I come across the API Specification page, which is clearly overwhelming
for someone like me. I realize understanding all this will take years. My question is I really don't grasp fundamentally how to use constructors or what
I put/or need after creating one.

For example, if I go under the button class the two constructors are:

Button()
Button(String label)

Below those are a bunch of methods, like getActionCommand(), getLabel(), etc.

My question is how do I know what is required once I input a class/method?

If part of my program is:



How do I know what exactly has to go below the Button? Does it come down to having to click on every link for each method or is there an overarching theme?
Similarly, if I then use getActionCommand() or a method within the class, how do I know exactly what goes in those also?
(I'm just using the Button() as an example because it's small but I mean for every constructor/method.)

 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand all this, you must first forget about the programming syntax of the language and focus on the concepts of what you are trying to understand...

Your first question should be... what is a class? ... what is this class stuff all about?

Well its simply a way to allow entities to be distinguishable... how you may ask... simply by observing its attributes and behaviours...

Entities that belong to the same class have common attributes and behaviours...

Now to get into the programming aspect of it we can think about it this way...

A class specifies a specific type which holds information about what data it can store and what operations can be performed on the data it stores...

Looking at the built in types such as int, double, boolean, etc... Each of these types fully describes what data it can store and exactly what operations can be performed on the data it stores...

Targeting the type int when you write something like -> int variable = 0; ... You know that the only data this type of entity can store are positive and negative whole numbers and you can perform addition, subtraction, etc on this type...

The rules for using this entity was predefined and you are forced use it in the way it was created along with all the other predefined entities that you happen to use...

To join in on the fun you also are allowed to create classes of entities specifying what information it can store and how it will behave...

You use the constructor to give the new type its born-into-the-world values for it attributes and the methods that you create for the new type tells the world what contributions it can make to its environment...

So with this information we can illustrate the purposes of these concepts:

Evolution is taking place and a new creature is in the making:


A new creature is now born into the world:


From this wacky example you can now see that a class is used to introduce a new type that has attributes and behaviours...

The constructor is simply used to set its attributes to specific values when its created

Methods are used to give it behaviour

This idea can be used in any context whether its purpose is for entertainment, business, media, communications, etc...
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol....thank you. That was really helpful and I appreciate you taking the time to write all that.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use that Button class; use JButton instead.

Just about every class should have at least one method and at least one constructor. Make sure all fields have been initialised to sensible values by the time the constructor completes; that will reduce the risk of nasty errors later. The commonest sort of error from not initialising things is that you have nulls around.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the syntax if I'm combining multiple classes? Meaning, in your example if you also added a class
like eat() and drink(), how would you code for:

urinator.grow().eat().drink() ?

I know that's wrong but don't know the right way or if there is one. Would I just have to make a fourth class that includes those three?

Thanks for all your help.
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be clear, are you referring to extending a Class or adding a method to an existing Class? There are only two Classes used in Rico's excellent example (GoldenUrinator and World), so I'm not sure what the third Class is you are referring to?

If you are not sure of the difference between a Class and a method, an over-simplified explanation:
Class - A Class is a thing. Basically, anything you can put "the" in front of is a Class. THE time, THE ball, THE urinator.
Method - A method is an action (or "doer") that a Class can do. time.set(), ball.drop(), urinator.grow().

I interpreted your question as wanting to add methods to the GoldenUrinator Class, not extending the GoldenUrinator Class.
If you are not sure of the terminology, I recommend you review the Java Tutorial on OOP Terminology.
That tutorial will also answer your question regarding the syntax of both methods and Classes.

However, if I am misreading the thread, I apologize. To answer your question as asked, Java does not support multiple inheritance. Therefore, you can only extend from a single base Class. You can, however, implement multiple Interfaces. The syntax to do this would be:

class Child extends Parent implements Interface1, Interface2

The link above will also provide additional examples, plus Interface and Class inheritance syntax.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some classes whose methods are written so you can daisy‑chain them like that. If you look at the documentation for StringJoiner[←link], it shows an example like that. To get that to work, you have to alter the methods in two ways:-
  • 1: Give them a return type the same as the class they are in.
  • 2: Give calling code a reference to the object you are calling the method on. That is done by adding a last line to the method saying “return this;”
  • If you go through the methods for classes like StringBuilder and PrintStream you find objects of those classes also return themselves from some of their methods. You can tell that because it doesn't say void on the left.

    There is a posh name for calling several methods like that but I have forgotten what it is.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ryan Bishop wrote:What is the syntax if I'm combining multiple classes? Meaning, in your example if you also added a class
    like eat() and drink(), how would you code for:
    urinator.grow().eat().drink() ?

    I think others have have covered most of it, but just FYI, that type of "daisy-chaining" is also called method "chaining" or "cascading", and it's the primary tool of Fluent Interfaces - something I'm rather fond of (although they can be abused).

    HIH

    Winston
     
    Ryan Bishop
    Ranch Hand
    Posts: 143
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm sorry and you're right. I should have put methods instead of classes. As always, thank you everyone for your help and the link. It's really helpful.
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    Ryan Bishop
    Ranch Hand
    Posts: 143
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What is the point of set/get methods? If I can do:



    then when/why would I do get/set grow()?

    I guess what it boils down to is I don't understand when I use a constructor, when I just do void whatever(), and when I do get/set methods. I'm admittedly a moron but they all seem to do the same thing to me.
    I genuinely have no idea what the difference is between doing a set/get vs just void grow() and haven't done any set/get on my own except for when completing a problem that specifically asks for it.

    Thanks for any advice.
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You would not set grow, because grow() is a method not a field. You only ever have setXXX methods to set the values of fields.
     
    Dave Tolls
    Ranch Foreman
    Posts: 3068
    37
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ryan Bishop wrote:
    I guess what it boils down to is I don't understand when I use a constructor, when I just do void whatever(), and when I do get/set methods. I'm admittedly a moron but they all seem to do the same thing to me.


    Ah, well, if they all did the same thing then we'd only need the one method.


    And no, you're not. It simply hasn't clicked yet.

    Ryan Bishop wrote:
    I genuinely have no idea what the difference is between doing a set/get vs just void grow() and haven't done any set/get on my own except for when completing a problem that specifically asks for it.

    Thanks for any advice.


    The difference is in what the methods themselves actually do.

    So, to use your example, your grow() method presumably does something involving growing (within the parameters of the problem you are trying to solve). If the class that has this method is, say, a model of a Tree then grow() would presumably do whatever it is that represents the Tree growing.

    In the case of setXXX or getXXX those (in Java) are generally very basic methods for setting or getting an attribute of the object in question. So, for our Tree, we might have a getHeight method which would return the height of the Tree. We could have a setHeight method, but do we really want to be telling the Tree how tall it is, and changing its height at will? Probably not, depends on the application. That's something that might, say in a game, be better handled by the grow() method which would likely have an algorithm for determining how much the Tree should grow.

    So to take the Tree as an example:


    Note I stuck a constructor in there so we could create a Tree with a certain age, which determines the initial height.

    So...grow() is a method that does stuff defined by what you're modelling.
    The getter allows you to get a value from the object in question.

    As you can (hopefully) see they do what they say they do, and so are not the same.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ryan Bishop wrote:What is the point of set/get methods? If I can do:then when/why would I do get/set grow()?

    A very good question. And, assuming - as Dave said - grow() is something that you're modelling, then you probably don't need separate set/get methods.

    Getters and setters are usually one of the first things that you're taught, because they provide a layer of protection for your fields; and also - in the case of setters - the ability to add "rules" for the proper setting of a field.

    Take Dave's tree. It seems highly likely that you're not going to want the 'age' field to be negative; and, to my knowledge, there are no trees on the planet that are over 5,000 years old, so you could add amethod.
    And if you always use it when you want to change the age, you can be pretty darn sure that the age will never be wrong.

    And making fields private, and providing "getter" methods instead protects your class from anyone inadvertently (or deliberately) changing one of your fields - which, incidentally, is why you should definitely think three times before you make any "setter" methods public.

    However, as you'll discover, set/get methods are very often superfluous, and simply end up cluttering up an API. I don't want to overload you too soon, but if you're interested, this is an excellent little article that explains why.

    HIH

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!