QA's approach 2 Java- Engineerig drawing analogy of Class vs Object

Class vs Object, an Engineering drawing Anology

Class is like an Engineering drawing

Assume class to be a Engineering drawing of vechile with various parts & features
In order to use a vehicle, we need to have an actual vehicle, so we need to Build it.

Inorder top use various features of vechile we need to Build an actual Vehicle
Similarly in order to use the members of class we need to build an object.

Using Brakes of vehicle is similar to Using methods of the Created object.

Reuse

Just as a car’s engineering drawings can be reused many times to build many cars, you can
reuse a class many times to build many objects. Reuse of existing classes when building new
classes and programs saves time and effort. Reuse also helps you build more reliable and
effective systems, because existing classes and components often have gone through extensive
testing, debugging and performance tuning.

State - Behavior and identity of an object

State:
represents data (value) of an object.

Every object has state, which are basically its attributes. like say for human object- various attributes are height, weight, color, etc.

Behavior:
represents the behavior (functionality) of an object.

An object can be considered a “thing” that can perform a set of related activities. The set of activities that the object performs defines the object’s behavior. Human object can walk,talk, work, etc.

Identity:
Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But, it is used internally by the JVM to identify each object uniquely.

Consider the motor car. If a car were designed as a single machine, it would be seen as hugely complex with lots of opportunities for failure. However, when broken down into its constituent parts, such as wheels, pedals, doors, etc. the individual design items become simpler. Each part (or object) is created independently, tested carefully and then assembled into the final product. The creation of the parts can be simplified further when they are broken down into even simpler items. For example, when each door is considered as being composed of an outer panel, handle, inner panel and window.

Instantiation

Just as someone has to build a car from its engineering drawings(Class) before you can actually
drive a car, Similarly one must build an object of a class before a program can perform the tasks that
the class’s methods define.
The process of doing this is called instantiation. An object is then referred to as an instance of its class.

What happens while creating an object? 

1.Memory is provided for the Object in Heap area
2.Initialization with default values provided by the JVM.
3.Constructor is called & initial values are overwritten.
4.Referenced Data type is created & the Object is mapped to this reference.


What are the different ways to create an object in Java?

There are many ways to create an object in java. They are:

o By new keyword
o By newInstance() method
o By clone() method
o By factory method etc

Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?