Scientech Easy Class and Objects in Java with Real-time examples | Scientech Easy

Sunday, July 22, 2018

Class and Objects in Java with Real-time examples

In this chapter, we will learn Class and Objects in Java in real time examples which is a basic and very important topic for beginners to understand the concept of core java programming. You know that Java is an object-oriented programming language. In the object-oriented programming language, we can design a program using class and objects. 
Let's see first the concepts of objects in Java.

Objects in Java

Definition:
An object is any real-world things that have properties and actions. In other words, an entity that has state and behavior is known as an object. Here, State represents the properties and Behaviour represents the actions or functionality.

An object has three characteristics:
1. State: It is represented by the instance variable/attribute of an object. It also represents the properties of objects. 
2. Behavior: It is represented by methods in Java. It represents functionality or actions. 
3. Identity: It represents the unique name of an object.

Let's take a real-world example to understand all these points clearly.
We are taking an example "person". A person has three characteristics: Identity(name), State i.e properties, and behavior i.e actions or functionality. Consider below image.
object in Java with real time examples,object,java object,what is object in java?
Class and objects in Java
In the above image, the First state/property of the person(object) is black hair which can be represented in Java like this: hairColor="black"
In the same way, the second property of the object is eye color which can be represented in Java like eyeColor="black" and so on. These are called attributes which define the properties of the person. 
Now if we consider Behavior or actions then actions/behavior of the person are "eat, sleep, walk, play, and study. These actions can be represented in Java like this: eat(), sleep(), walk(), play(), and study(). These are called methods. 
Remember, there are many properties and actions of any persons. we have just shown few of them.
Thus, properties and actions are combined together for any real-world objects make an object in Java. (Remember)
Note: Noun in English represents properties or state of an object and it can be represented with the help of variables whereas verbs represent action/behavior which can be represented with the help of methods.

Let's take one more interesting example 'Phone Samsung Galaxy'. If I tell you to represents this phone in Java in the form of an object then How will you do like that?
You can do that very easily. Ask two questions: 
What it has? (Properties)   and What it does? (Actions)
Properties are width=6.2 or  "6.2 inch", height=13.6 or "13.6 cm", color="black", os="Android", price=1000 or "1000$", brand="Samsung", and weight="130 gm". All these are nouns called attributes. 
Actions/Behavior are call(), sendSms(), runApp(), browserInternet(), sharing(). Thus, a phone object is comprised of the properties that the phone has and actions that phone does.

Another example we can take "Pencil". A pencil is an object. Its name is Natraj. State: the color is black. Behavior: It is used to write so writing is behavior. 
Thus, You can take any object around you and think about what property it has? and What action it does?

Class in Java

 Definition: 
In Java, a class is basically user-defined data types which act as a template for creating an object. It represents the common properties and actions(functions) of an object. In other words, we can also define as "a class is a group of objects which are common to all objects of one type".
Note: A class is the type of an object. (Don't forget it)

First Real-time example of class
Let us consider two objects Samsung Galaxy S4 and iPhone. Suppose Samsung Galaxy S4 have some properties like width="6.98 cms", height="13.6 cm", OS="Android", brand="Samsung", price="1000$" and Actions are call(), sendMessage(), browser(), share(). 
Now, suppose iPhone has some properties such as width="5.86 cm", height="12.3 cms", OS="iOS", brand="Apple", price="1200$" and actions are call(), sendMessage(), browse(), share(). 
Both objects have some different properties and actions but the type is the same "Phone". This is the class. i.e the name of the class is "Phone".

Second Real-time example
Consider two objects one boy and one girl. The boy has some properties like hairColor="black", eyeColor="black", skinColor="Fair", height="5.10 inch", weight="65 kg" and actions are read(), play(), sleep(), walk(). 
Now, The girl has some properties like hairColor="Brown", eyeColor="brown", skinColor="milky white", height="5.4 inch", weight="50 kg" and actions are read(), play(), sleep(), walk(). but the type of both is the same. i,e Person. So the class name is 'Person'.

In general, a class can have the following components to act as a template:
1. Modifiers: A class can be public or default.
2. Class name: By convention, a class name should begin with a capital letter and subsequent characters lowercased(for example Student). If a name consists of multiple words, the first letter of each word should be uppercased( for example CollegerStudent).
2. Body: Every class's body is enclosed in a pair of left and right braces. In the body,  a class can contain:

Fields: Fields are the variables that provide the state/properties of the class and its object. It may be a Local variable, Instance variable or Static variable.
Constructor: Constructors are used to initializing the values of variables and new objects. A constructor can be divided into two types such as Default constructor and User-defined constructor.
Methods: methods are used to implement the actions or behavior of the class and its object. It may be an Instance method or a Static method.
Blocks: blocks are mostly used to change the default values of variables. It may be an instance block or static block.
Interface: It is used to achieve the multiple inheritances in Java. 

Syntax to declare a class

modifiers_name class class_name {
    // Class body.
         Variables;
         constructors;
         methods;
         Instance blocks
         Static blocks
    }
where modifier_name can be only public or default. If you declare a class as public then it can be accessed from anywhere but if you do not declare the class as public then it will be considered as default and will be accessed within the same package only. 
Note: Object is the instance of a class.

Flow of execution in Java program

➲ Out of five elements declared inside the class body, the Static block is executed first during the loading of dot class file.
➲ During the object creation, first, the instance block is executed before the execution of a constructor.
➲ After the execution of an instance block, the constructor part will be executed.
➲ In the case of a method, First, the Static method is executed during the class loading and then the instance method is executed during the object creation.
➲ Similarly, the static variable is executed during the class loading and the instance variable is executed during the object creation.
➲ The local variable is executed inside the method, constructor or block.

A simple class example
Suppose, Student is a class name and the name, rollNo, id, age of the students will be its properties. The "public" is the access modifiers. Let's see these in Java syntax.

    public class Student { // Declaration of state/Properties. String name; // Instance variable. int rollNo; // Instance variable. int id; int age; // Declaration of constructor. Student() { // Constructor body. } // Declaration of Actions. void display() { // method body. } ------ ------ }
Final words:
I hope that this article helps you to understand the concept of Class and Objects in Java with real-time examples. The real-time example can understand you the concept if you imagine in your mind and think about it sometimes.

More on Object and Classes
1. How to create an object in Java with example.
2. Object Declaration and Initialization in Java
3. The life cycle of Object in Java


Next ➝ How to create an object in Java                                                              Scientech Easy loves you