Thursday, December 6, 2012

What is Object in Java Programming and OOPS - Example Tutorial

Object in Java
Object in Java programming language or any other Object oriented programming language like C++ are core of OOPS concept and that's why the name. Class and Object along with Inheritance, Polymorphism, Abstraction and Encapsulation forms basis of any Object oriented programming language e.g. Java. Objects are instances of Class, Class define blue prints and Objects are thing which are created based upon that blueprint. Object are also known as instance in Java, e.g. When we say an instance of String class, we actually mean an Object of String class. Object has state and behavior in Java. State is represented using instance variable and static variable in Java class and behaviors are implemented using methods in Java. What differentiate two Objects of same class are there state e.g. if we take two instance of String class "A" and "B" there contents are different which is there state. In OOPS programming,  we model real world things into Class and Object e.g. Car class is a blue print of car which may specify that car should have 4 gears, 4 seats an engine etc and every single car is an Object or instance of Car class. If you like to learn more on OOPS concepts and designs, I suggest to read 10 OOPS and SOLID design principles in Java, That will also help to improve your OOPS programming.

How to create Object in Java
There are multiple ways to create Objects in Java e.g. Reflection, Serialization, Cloning etc but most common and easy way to create Object in Java is by using new() keyword. When we create an object of any class in Java, its constructor gets called, which initialized object with its default or initial state. Since a class can contain overloaded constructors in Java, you can also invoke any particular constructor by using new() keyword with argument list of that constructor. for example String class has multiple overloaded Constructors, one of them accept another String instance. You can also use Factory methods to creates Objects in Java. Factory methods are created as part of Factory design pattern which is a Creational design pattern in Java and best suited to create instance of Immutable classes.

Difference between Class and Object in Java

What is Object in Java and OOPS with Example Class vs Object
Many Java programmers especially beginners confused between Class and Object. When I started programming, I was also on same boat and don't understand difference between Class and Object in Java, even after reading there definition. To be fair, it does look simple but understanding OOPS concept takes some time. Anyway, main difference between Class and Object in Java is that Class is a blueprint or a model while Objects are actual things which are created out of those blueprint or model. Best way to understand this is, thinking in terms of model and design. Before actually building a Car or Vehicle a design is created which specifies How many gears car should have, how many seats, how it will start, stop etc. All details of it is captured. This is a class which can be created using class keyword in Java. Actual cars which will be produced based upon those blueprints are objects. If  you want to differentiate Class and Object in Java code, then you can not because all you see is code, wrapped inside a class. Objects are created at runtime when you start JVM with java command and JVM will start executing your code, whenever JVM encountered new keyword it will create an Object.

Important points related to objects in Java
Some points related to Objects in Java which is worth remembering e.g.

1) Objects can be serialized in Java by using Serialization process, which stores state of Object into persistence e.g. file or database so that later in time, same Object can be recreated.

2) Classes which have just one Object or one instance in whole application are known as Singleton in Java. It's tricky to create thread-safe Singleton in Java but they are very useful. One example of Singleton in Java is java.lang.Runtime

3) Java programming language provides mechanism which converts primitive data types like int, long, double into respective Objects e.g. Integer, Long and Double and this process is called Autoboxing in Java. opposite of converting Object to primitive is known as Unboxing in Java.

4) Objects in Java are created in heap memory, which is allocated when we start JVM by java command.

5) variable which points to any instance or object in Java are known as reference variable e.g. in following example :

String name = new String("Java");

name is a reference variable which points to an String object whose contents are "Java".

6) Java API also has a class called java.lang.Object in java.lang package which is Super class of all Java classes. Every class in Java including System and user defined classes implicitly extends Objects, if they don't extend any other class explicitly.

7) Objects which are unreachable by any Thread in JVM is known as dead objects are eligible for Garbage collection. When garbage collector runs, it cleans those objects and reclaim memory from them.

These were some fundamentals related to What is an Object in Java and how to create Objects in Java. If you are new to Object oriented programming, it's in best interested try to see real world in terms of Class and Object and find out What is a class and What is an Object. When you write program, that understanding will help you to decompose a big problem into smaller one which can be represented using class and objects in Java.

Further Learning
Java Fundamentals, Part 1 and 2
SOLID Principles of Object Oriented Design
Head First Design Pattern

Other OOPS and design tutorial for Java programmers

No comments :

Post a Comment