Understanding Classes and Objects in Java
The term Object-Oriented explains the concept of organizing the software as a combination of different types of objects that incorporates both data and behavior. Hence, Object_oriented programming(OOPs) is a programming model, that simplifies software development and maintenance by providing some rules. Programs are organised around objects rather than action and logic. It increases the flexibility and maintainability of the program. Understanding the working of the program becomes easier, as OOPs brings data and its behavior(methods) into a single(objects) location.
Basic concepts of OOPs are:
This article deals with Objects and Classes in Java.
Requirements of Classes and Objects in Object Oriented Programming
Classes: A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. Classes are required in OOPs because:
- It provides template for creating objects, which can bind code into data.
- It has definitions of methods and data.
- It supports inheritance property of Object Oriented Programming and hence can maintain class hierarchy.
- It helps in maintaining the access specifications of member variables.
Objects: It is the basic unit of Object Oriented Programming and it represents the real life entities.
Real-life entities share two characteristics: they all have attributes and behavior.
An object consists of:
- State: It is represented by attributes of an object. It also shows properties of an object.
- Behavior: It is represented by methods of an object. It shows response of an object with other objects.
- Identity: It gives a unique name to an object. It also grants permission to one object to interact with other objects.
Objects are required in OOPs because they can be created to call a non-static function which are not present inside the Main Method but present inside the Class and also provide the name to the space which is being used to store the data.
Example : For addition of two numbers, it is required to store the two numbers separately from each other, so that they can be picked and the desired operations can be performed on them. Hence creating two different objects to store the two numbers will be an ideal solution for this scenario.
Example to demonstrate the use of Objects and classes in OOPs
Objects relate to things found in the real world. For example, a graphics program may have objects such as “circle”, “square”, “menu”. An online shopping system might have objects such as “shopping cart”, “customer”, and “product”.
Declaring Objects (Acalled instantiating a class)
When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.
As we declare variables like (type name;). This notifies the compiler that we will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variable, type must be strictly a concrete class name. In general, we can’t create objects of an abstract class or an interface.
If we declare reference variable(tuffy) like this, its value will be undetermined(null) until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object.
Initializing an object using new
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.
Hi my name is tuffy. My breed, age and color are papillon, 5, white
- This class contains a single constructor. We can recognize a constructor because its declaration uses the same name as the class and it has no return type. The Java compiler differentiates the constructors based on the number and the type of the arguments. The constructor in the Dog class takes four arguments. The following statement provides “tuffy”, “papillon”, 5, “white” as values for those arguments:
Dog tuffy = new Dog("tuffy", "papillon", 5, "white");
The result of executing this statement can be illustrated as :
Note : All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, also called the default constructor. This default constructor calls the class parent’s no-argument constructor (as it contain only one statement i.e super();), or the Object class constructor if the class has no other parent (as Object class is parent of all classes either directly or indirectly).
Different ways to create Objects
- Using new keyword: It is the simplest way to create object. By using this method, the desired constructor can be called.
ClassName ReferenceVariable = new ClassName();
Bravo, 4 Oliver, 5
- Using Class.newInstance() method: It is used to create new class dynamically. It can invoke any no-argument constructor. This method return class Class object on which newInstance() method is called, which will return the object of that class which is being passed as command line argument.
Reason for different exceptions raised:-
ClassNotFoundException will occur if the passed class doesn’t exist.
InstantiationException will occur, if the passed class doesn’t contain default constructor as newInstance() method internally calls the default constructor of that particular class.
IllegalAccessException will occur, if the driving class doesn’t has the access to the definition of specified class definition.
ClassName ReferenceVariable = (ClassName) Class.forName("PackageName.ClassName").newInstance();
Hello Geeks !!
- Using newInstance() method for Constructor class: It is a reflective way to create object. By using it one can call parameterized and private constructor. It wraps the thrown exception with an InvocationTargetException. It is used by different frameworks- Spring, Hibernate, Struts etc. Constructor.newInstance() method is preferred over Class.newInstance() method.
Constructor constructor = ClassName.class.getConstructor(); ClassName ReferenceVariable = constructor.newInstance();
- Using clone() method: It is used to make clone of an object. It is the easiest and most efficient way to copy an object. In code, java.lang.Cloneable interface must be implemented by the class whose object clone is to be created. If Cloneable interface is not implemented, clone() method generates CloneNotSupportedException.
ClassName ReferenceVariable = (ClassName) ReferenceVariable.clone();
201, Tom 201, Tom
- Using deserialization: To deserialize an object, first implement a serializable interface in the class. No constructor is used to create an object in this method.
ObjectInputStream in = new ObjectInputStream(new FileInputStream(FileName)); ClassName ReferenceVariable = (ClassName) in.readObject();
Differences between Objects and Classes