Featured post

Learning Programming is Actually a fun.

Yes!! Learning programming is very fun you just need to feel the fun instead of taking pressure on learning. For the past years I'm trying to learn to program, first I started with Java for few months then left java and started learning Python after spending 5-6 months once again I moved to Java. It's kinds of not stable for me but now I'm stable at Learning java and trying hard to learn.

For the past 1 year, I'm learning Java, along with my Job and Freelance Work. I just don't get enough time to study much, so it's kinds taking long but still, I'm trying hard, hoping there will be a day for me as a Java Programming as I believe learning has no age.

So now coming to point, recently I was learning about the Classes in java and I found a very interesting way of understanding the Classes in java.


It's actually a Comment in the Video and all credit goes to the writer Clement Chang.

Here is what he explained What is the Classe in JAVA?

Think of yourself as the god. And you can do everything. Now you want to create an object in this new world. For example, human beings. However, before you create human, you have to design them first. And this design blueprint is the class in Java. Every human in this world have to follow this blueprint, or, has the feature written on this blueprint.

In this blueprint, you define a lot of detailed things. For example, what do human have and what can they do? Human should have a name, a sex type (male or female), 1 head, 2 legs, and 2 arms, etc. And they can walk, they can run, and some of them can even swim. The thing they have are what's called attributes or data fields. And what they can do is known as behavior or methods in Java. 

so you may define a class named Human in Java like this:

Take this class for instance. If you use this class as the blueprint of Human, then every human would have 100 in HP, 100 in stamina when you create them. And besides, every human can run and sleep. Run will cost the stamina and sleep can recover the energy. However, this is just a blueprint. To create a human, you have to say something like:

Human adam; 

That's how you create a new object of Human. Notice here we did not use the constructor. Why do you -- the god, need the constructor? Since you only defined the default value of HP and stamina but name and sex, you have to use the constructor to set these two value for every object using parameters:

Human eva = new Human("Eva", "Female"); 

And this Human "Instance(or Object)" Eva, got a name Eva, and she is a female. She can run and sleep, as you defined in the Human class.

notice here "Eva" isn't the name of this object, but a reference, a nickname. When you need to use this object, you have to use this nickname to cite it. But the actual name you set for this human is what you said in the constructor -- "Eva".


Thanks, Clement Chang 


Comments