The Best Technical Blog Ever

twitterrss

Java -Objects and Classes

In this tutorial, we will learn about java objects and classes. Object is the physical as well as logical entity whereas class is the logical entity only.


Object in Java:

Object is an entity(thing) that encapsulates state and behavior. It can be physical or logical (tangible and intangible). We can find many real-world objects around us such as Cars, tables, Dogs, pen, etc. All these objects have a state and behavior. An object has three characteristics:


  • state: represents data (value) of an object.

  • behavior: represents the functionality of an object such as running, deposit, withdraw, turn_left etc.

  • identity: This unique ID is used internally by the JVM to identify each object uniquely.

For Example:
  1. Considering a dog:- here state -name, breed, color, and the behavior - barking, running , wagging
  2. Pen object :-state- name( Pilot pen), color (Black) etc and behavior- writing, drawing.

When comparing this real world object to the software object, Software objects also have a state and behavior. A software object's state is stored in fields and behavior is explained via methods. In software development, methods manipulate the object's state and the object-to-object communication is also possible via methods.


An object is an instance of a class created using a new operator. If you think of a factory producing motorbikes, then the class would be the blueprint / manual of how the finished motorbikes should look, and the objects would be each of the physical produced motorbikes.



Classes in Java:

A class is a group of objects that has common properties. It is a model or template or blueprint from which objects are created. A class contains data field and method to define the state and behavior of its object.


Syntax for Declaring Class:

<Access Modifier> class <class_name>{  
    data member;  
    method;  
}  

Here, Access modifier defines who can access the class and it's members of the class. Class_Name is the unique name in a specific package.


Example of Class
class

A java class can contain:

  • fields (data member)
  • method
  • constructor

Fields

A field is a piece of data stored in a variable that belongs to a class. For example, a student class could define the field name which all student objects would have. Each Student object could then have a different value for the name field.


Example:
field

Here is the Student class with a field name. This example declares a fieldname of data type String. A class can contain any of the following variable types.

  • Local variables
  • Instance variables
  • Class/static variables.

Methods

Methods are groups of operations that carry out a certain function together. They are used to expose behaviour of an object. For example, a method may perform addition , subtraction of numbers, Or, a method could read and write data in a database etc. In other programming languages methods may be called "procedures" or "functions". Main advantage is code reusability.


Example:
method

Here is the Student class with a method named getName.


Constructors

Every class has a constructor. If we do not explicitly write a constructor for a class, then the Java compiler builds a default constructor for that class. Constructors are a special kind of method that is executed when an object of that class is created. It is used to initialize the objects. Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.


Example:
constructor


Creating an Object:

An object is created from a class using new keyword. The new operator returns a reference to a new instance of a class and can be assigned to a reference variable of the class. The process of creating objects from a class is called object instantiation. Each time you create an object, a new set of instance variables comes into existence which defines the characteristics of that object. There are three steps when creating an object from a class:


  • Declaration: A variable declaration with a variable name with an object type.
  • Instantiation: The 'new' key word is used to create the object.
  • Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new object.

Syntax:
< Class_Name > ClassObjectReference = new < Class_Name >();

Example
object

Here constructor of the class Student will get executed and object will be created. Object Name(stud1) will hold the reference of created object in memory.


class and object example

There are many ways to create an object in java.

  • By new keyword
  • By newInstance() method /Reflection- If we know the name of the class & if it has a public default constructor we can create an object in this way.
  • By clone() method - used to create a copy of an existing object.
  • By Deseriallization
  • By ClassLoader

Accessing Data Members and Methods:

We can access Data members and methods by using objects. To access a data member of an object, First create an object and call a data member or class method by using object name followed by dot followed by data member/ name of the method and its argument list.


Syntax:
objectName.Datamember
objectName.methodName(arg1,arg2)

Example
accessing data member


Anonymous object

An object that have no reference(nameless object) is known as anonymous object. To use an object only once, anonymous object is a better way


Example:
anonymousobject


Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status