V.3.0. - OOP in Java - Get Your Hands Dirty With Code
V.3.0. - OOP in Java - Get Your Hands Dirty With Code
(Don’t just copy and paste the codes, type the codes like there’s no
tomorrow)
Objects are born and objects die. You are in charge of an object’s lifecycle. You
decide when and how to construct it. You decide when to destroy it (you don’t
actually destroy the object yourself, you simply abandon it and Java GC i.e. Java
Garbage Collector will do the rest).
Human humanOne
It looks like we’re calling a method named Human(), because of the parentheses.
Actually we are calling the Human Constructor.
A constructor does look and feel a lot like a method, but it’s not a method. A few
are some differences exist:
public Human() {
// constructor code goes here
}
The only way to invoke a constructor is with the keyword new followed by the
“class name”.
You can write a constructor for your class (we’re about to do that), but if you don’t,
the compiler writes one for you!
public Human() {
// constructor code goes here
}
A constructor has the code that runs when you instantiate an object. In other
words, the code that runs when you say new on a class type.
Every class you create has a constructor, even if you don’t write it yourself.
Uses of constructors:
Mostly used initialize the state of an object i.e. assign values to the object’s
instance variables.
public class Human {
// instance variable
int age;
// constructor
public Human() {
System.out.println("Hello World!!!");
}
}
Well, you could add a “setAge()” setter method to the class. But that leaves the
Human temporarily without a age (instance variables do have a default value. 0 or
0.0 for numeric primitives, false for booleans, and null for references), and forces
the Human user to write two statements—one to create the “Human”, and one to
call the “setAge()” method. The code below uses a setter method to set the initial
age of the new Human.
public class Human {
int age;
public Human() {
System.out.println("Hello World!!!");
}
Object shouldn’t be used until you’re finished initializing of its state (instance
variables). Let someone make and get a reference to a new Human object that isn’t
quite ready for use that’s not a good idea. How will the Human-user even know that
he’s required to call the setter method after making the new Human?
Solution to this problem is put initialization code is in the constructor. And all you
need to do is make a constructor with arguments.
public class Human {
int age;
}
}
If you write a constructor that takes arguments, and you still want a no-
arg constructor, you’ll have to build the no-arg constructor yourself!
The compiler gets involved with constructor-making only if you don’t say anything
at all about constructors.
Imagine that you want Human users to have 2 options for making a Human—one
where they supply the Human age (as the constructor argument) like before and
one where they don’t specify a age and thus get your default Human age.
You can’t do this cleanly with just a single constructor. Remember, if a method (or
constructor—same rules) has a parameter, you must pass an appropriate argument
when you invoke that method or constructor.
If someone doesn’t pass anything to the constructor, they won’t even be able to
compile without sending an int argument to the constructor call.
Not a very good solution: If the parameter value is zero, give the new Human a
default age, otherwise use the parameter value for the age.
public class Human {
int age;
That means the programmer making a new Human object has to know that passing
a “0” is the protocol for getting the default Human age! What if the other
programmer doesn’t know that? Or what if he really does want a zero-age Human
(Assuming a zero-age Human is allowed)? So it might not always be possible to
distinguish between a genuine “I want zero for the age” constructor argument and
a “I’m sending zero so you’ll give me the default age, whatever that is” constructor
argument. That’s why the above solution isn’t very good one!!!
To solve this problem you really need to 2 ways to make a new Human!
You need two constructors. One that takes an int and one that doesn’t. If you have
more than one constructor in a class, it means you have overloaded constructors.
public class AnotherHuman {
int age;