Class in Java

By | July 26, 2020

Class is a blueprint or master-copy using which we can create an Object. It’s not any physical entity.

Before going into a detailed explanation let me tell you a small story.

Long years back there was a man, who lived around 500 miles away from his home. He very badly missed her family, at that time there was no source of telephonic communication, such as mobile or cell phones.

One night when he was sitting alone under the tree, suddenly a question clicked on his mind. He was thinking “Is there no way to talk to my family sitting long miles away from me?”

That question was always poking in his mind. So, he started thinking and he made a commitment to himself that he gonna be found some solution to fix this issue.

He took a piece of paper and started drawing some random design, finally after investing his lots of hours and days, he finalized one design for the device, on that paper.

The following are things he wrote it down on the paper.

FeaturePropertyUsage
Numeric ButtonsBlackTo dial.
Speaker ChipBlueTo speak during call.
BatteryGrayTo charge the device.

Finally, he gave his design to the manufacturing company to develop that device. Meanwhile, he was thinking of a good name for that device, finally, he named Telephone.

I hope you are enjoying the story and wondering about what hell this story is?? How is going to related to Java Class?? Don’t worry if you understood the whole story till now then you understand the Class concept too.

Still thinking how you understood the whole Class Concept without even reading a single technical thing related to that, let’s look into below.

Here the design is nothing but a blueprint of the whole device which going to be manufactured. This is nothing but a Class, Yes you read correctly this is only Class in Java. Thus it’s clear that class is not any physical entity, it’s just whole structure about the thing which is going to look like.

And here the properties and usage is just a variable and method of Class respectively.

 So, once that telephone gets manufactured, then that is considered to be an Object in Java Context.

Hence, It has been proved that without Class we cannot able to create an Object because design or blueprint always required for developing any physical entity.

 

A physical entity is nothing but an Object in Java.

 Important points About Class

  • A class is a master-copy or blueprint using which we can create multiple objects.
  • Without Class, we cannot create any Object.

Note: In Video I explained with another example, please look into it.

Association in Java

It is a process of one or many objects getting associated with another Object. It is commonly used in Object-Oriented Feature.

There are two forms of Association

  1. Aggregation
  2. Composition

Note: Association is also called HAS-A Relationship.

Aggregation

  1. It is also a special form of Association wherein associated objects can still logically independently exist on its own even without the owner object.
  2. Aggregation is also called as Weak HAS-A-RELATIONSHIP.

Example of Aggregation

  • Employee and Company.
  • Music Playlist and Music.

Composition

  1. It is a special form of Association wherein an associated object cannot logically independently exist on its own without owner Object. If owner object is destroyed then automatically all the associated objects get destroyed.
  2. The composition is also called as Strong HAS-A Relationship.

Example of Composition

  1. Fish and water.
  2. A pop-up window (Frame) and Button objects on it.

Thank You So much for reading, if you have any queries or doubt please feel free to ask in the comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *