Object in Java
1. What is OOPS?
Object-Oriented Programming System is the programming technique to write programs based on the real world objects. The states and behaviors of an object are represented as the member variables and methods. In OOPS programming programs are organized around objects and data rather than actions and logic.
2. Difference between Procedural programming and OOPS
- A procedural language is based on functions object-oriented language is based on real-world objects.
- Procedural language gives importance to the sequence of function execution but object-oriented language gives importance on states and behaviors of the objects.
- Procedural language exposes the data to the entire program but object-oriented language encapsulates the data.
- Procedural language follows a top-down programming paradigm but object-oriented language follows a bottom-up programming paradigm.
- A procedural language is complex in nature so it is difficult to modify, extend and maintain but an object-oriented language is less complex in nature so it is easier to modify, extend and maintain.
- Procedural language provides less scope of code reuse but object-oriented language provides more scope of code reuse.
3. What is an Object?
The Object is the real-time entity having some state and behavior. In Java, Object is an instance of the class having the instance variables as the state of the object and the methods as the behavior of the object. The object of a class can be created by using the new keyword in Java Programming language.A class is a template or blueprint from which objects are created. So, an object is the instance(result) of a class.Various Object Definitions:1. An object is a real-world entity.2. An object is a runtime entity.3. The object is an entity which has state and behavior.4. The object is an instance of a class.
Various Object Definitions:
1. An object is a real-world entity.
2. An object is a runtime entity.
3. The object is an entity which has state and behavior.
4. The object is an instance of a class.
Real-world examples
- Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Chair, Bike, Marker, Pen, Table, Car, Book, Apple, Bag etc. It can be physical or logical (tangible and intangible).
Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Chair, Bike, Marker, Pen, Table, Car, Book, Apple, Bag etc. It can be physical or logical (tangible and intangible).
4. What are the advantages of using Software Objects
Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.
How to Declare, Create and Initialize an Object in Java
A class is a blueprint for Object, you can create an object from a class. Let's take Student class and try to create Java object for it.
Let's create a simple Student class which has name and college fields. Let's write a program to create declare, create and initialize a Student object in Java.
Let's create a simple Student class which has name and college fields. Let's write a program to create declare, create and initialize a Student object in Java.
public class Student {
private String name;
private String college;
public Student(String name, String college) {
super();
this.name = name;
this.college = college;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public static void main(String[] args) {
Student student = new Student("Ramesh", "BVB");
Student student2 = new Student("Prakash", "GEC");
Student student3 = new Student("Pramod", "IIT");
}
}
The Student objects are:
Student student = new Student("Ramesh", "BVB");
Student student2 = new Student("Prakash", "GEC");
Student student3 = new Student("Pramod", "IIT");
Each of these statements has three parts (discussed in detail below):
Declaration: The code Student student; declarations that associate a variable name with an object type.
Instantiation: The new keyword is a Java operator that creates the object.
Initialization: The new operator is followed by a call to a constructor, which initializes the new object.
5. Declaring a Variable to Refer to an Object
General syntax:
type name;
This notifies the compiler that you will use a name to refer to data whose type is a type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable.
From the above program, we can declare variables to refer to an object as:
Student student;
Student student2;
Student student3;
Instantiating a Class
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 object constructor.For example:Student student = new Student("Ramesh", "BVB");
Student student2 = new Student("Prakash", "GEC");
Student student3 = new Student("Pramod", "IIT");
Note that we have used a new keyword to create Student objects.
Student student = new Student("Ramesh", "BVB");
Student student2 = new Student("Prakash", "GEC");
Student student3 = new Student("Pramod", "IIT");
Initializing an Object
The new keyword is followed by a call to a constructor, which initializes the new object. For example:
Student student = new Student("Ramesh", "BVB");
Student student2 = new Student("Prakash", "GEC");
Student student3 = new Student("Pramod", "IIT");
From above code will call below constructor in Student class.
public class Student {
private String name;
private String college;
public Student(String name, String college) {
super();
this.name = name;
this.college = college;
}
}