There's a saying that goes:
"Each head is a whole different world."
Which I find to be true. Some may argue that a certain teacher doesn't know...
For further actions, you may consider blocking this person and/or reporting abuse
The "prefix" variable pattern is one I mention often in the interviews I do. It's a strong indicator that there's a class hiding in your code: anytime you have multiple sets of variables with a common prefix, or suffix.
I'm sure this will be helpful to many 🙂
This is great, well done! I think an important benefit of Classes that can be added in the final section of this article is that you can create a method that takes a parameter of type
Person
and callsprintln
using the parameter's getters, then you can use this function on each instance ofPerson
, i.e. chris and daniel, without writing outprintln
twice.Thanks Alpha!
I'll edit the post to add your suggestion 🙌
Nice post, well explained, i like it, if i would teach in a future some OO language i would go back to this post as a reference. I would like to add two little conceptual details.
In Java a default constructor is constructor that gets generated if you don't explicitly create one. That's the reason why you can create and no-param object without creating an explicitly constructor.
Finally, i would like to add that in the above example "chris" works as a reference of an object assigned to an object in memory.
Thanks for the tips, Gustavo.
Regarding the
default constructor
, I thought it would be a correct way of calling it since that implementation of the Personclass
does not allow the client of our code to create an object without passing any arguments to it. Would it be better to say "overriding the default constructor"?Thanks to you, i really like it as an introduction for classes, you should continue doing this kind of post.
I considered can be called just param-constructor, if we explicitly add no-args-contructor can be called as a overloaded constructor. The Overrinding is not possible for constructors because the behavior strictly belongs to the class.
Nice post!
From my point of view, the main concern of classes is joining behavior and data into an encapsulated piece of code.
This split the code into cohesive pieces and decreases coupling. Two good properties of maintainable code.
The caller doesn't know how the behavior is implemented it only cares about what the method does.
Creating a class as a bag of data and expose data through getters and setters is not object-oriented since it delegates the manipulation of the data to the caller. That is procedural code using classes.
My suggestion for making your final example a little more OOP is:
Move
System.out.println
from Person class to caller class (Main).Remove all getters from Person class. (Nobody uses them and nobody should)
Implement
to_string
method instead of printInfoChristian -- terrific walkthrough of the practicalities of classes (as opposed to the abstract concept thereof that seems to be the canonical explanation for no good reason)!
Whenever I explain classes to the unacquainted, I find it useful to compare classes to a brand new data type that we, as users of any given language, get to create ourselves! This might be an imperfect analogy, depending on the language, but we all learn the basic primitive types when we first start (long before we get to groking classes). Later, we all develop some intuition about how those types behave. Ergo, a custom type (i.e. a class) is just a type which we get to direct!
That's a really nice way of looking at it too!
Nicely explained! I am missing one thing though: the cohesion also implies a common lifetime of the objects. One of the things where behavior gets actually different from comparing classes vs. prefixed variables.
Ok! Can we now discuss the tradeoffs between using a hash tables vs classes? POJOs and their kin are basically just heavy tuples/hash entries... What do you gain when you use a class vs. a lookup table? What do you lose?
Not to troll or sound douchy but it would be more helpful if you shared more context and what you thought about classes before and after. The reason is because a "classification" (i.e., the concept of a class) is such a fundamental concept to being human, that this post is super surprising. For example, my toddler learned that fido and rover are both types of dogs, that is, he knows about the class of dogs. Later in school, he will learn that people and dogs are both mammals, another class.
Not surprisingly when I first learned Java many years ago, i took to classes like a fish to water. I realized that it made programming more relate-able i.e., easier to understand. As for my context, I had previous experience with procedural programming, which made OOP a refreshing, more natural alternative. Maybe that's what made classes so hard for you?
Where I did struggle is with overengineering---that is, not everything should be a class (rushing to abstractions to early). Later I also learned from experience that using classes for code sharing can be bad in large projects---you get too much coupling. That means shallow hierarchies are preferable, and object composition for code sharing is even better.
👍🏼
Great post! But, one thing: instead of using ´printInfo´, you could simply overload the toString method and print out the object.