In: Computer Science
Answer 1:
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:
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
Declaring Objects (Also called 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.
Dog tuffy;
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.
answer 2:
Classes and Objects are basic concepts of Object Oriented
Programming which revolve around the real life entities.
Class
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. In general, class
declarations can include these components, in order:
Constructors are used for initializing new objects. Fields are
variables that provides the state of the class and its objects, and
methods are used to implement the behavior of the class and its
objects.
There are various types of classes that are used in real time
applications such as nested classes, anonymous classes, lambda
expressions.
Methods always part of an object or a class (for static methods you don’t need an instance/object).
In Java 8 with functional interfaces you can write such syntax where it looks like methods (lambdas) passed by as an argument just like an object. It is indeed a functional paradigm injected into the language but behind the scene an instance/object is created from the given lambda and this instance’s method will have the lambda function as the implementation.
An object in Java has two parts: