Classes and Objects
Classes and Objects
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.
Object - It is a basic unit of Object Oriented Programming and represents the real life entities.
➢ An object consists of -
1. State: It is represented by attributes of an object. It also reflects the properties of an object.
2. Behavior: It is represented by methods of an object. It also reflects the response of an object
with other objects.
3. Identity: It gives a unique name to an object and enables one object to interact with other
objects.
➢ Example of an object : dog
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.
➢ The state (value of those attributes) are unique for each object.
➢ A single class may have any number of instances.
➢ Example -
Dog dog_name; // This is just the referance variable. Object is NOT created (Memory is Not
allocated).
Initializing an object -
➢ 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.
➢ Example - Dog dog_name = new Dog(); // Now memory is allocated and the object is created.
Constructor -
➢ 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).
➢ If Parent Class doesn’t have any no-argument constructor, we need to explicitally call its
parametrized constructor inside Child Class constructor.
Anonymous objects -Anonymous objects are the objects that are instantiated but are not stored in
a reference variable.
➢ They are used for immediate method calling.
➢ They will be destroyed after method calling.
➢ Example: FileInputStream file = new FileInputStream(new File(filename)); // File object is
Anonymous.
Methods in Java - A method is a collection of statements that perform some specific task and
return the result to the caller. They allow us to reuse the code without retyping the code.
➢ In general, method declarations has six components :
1. Access Modifiers 2. Return type
3. Method name 4. Parameter’s list
5. Exception list 6. Method body
➢ Method signature: It consists of the method name and a parameter list (number of
parameters, type of the parameters and order of the parameters). The return type and exceptions are
not considered as part of it.
Access Modifiers – Defines access type of the method i.e. from where it can be accessed in your
application.