Classes and Objects:
In Java, classes and objects are basic concepts of Object-Oriented Programming (OOPs) that
are used to represent real-world concepts and entities.
• A class is a template to create objects having similar properties and behaviour,
or in other words, we can say that a class is a blueprint for objects.
• An object is an instance of a class. For example, the animal type Dog is a class,
while a particular dog named Tommy is an object of the Dog class.
Properties of Java Classes
• Class is not a real-world entity. It is just a template or blueprint, or a prototype
from which objects are created.
• A class itself does not occupy memory for its attributes and methods until an
object is instantiated.
• A Class in Java can contain Data members, methods, a Constructor, Nested
classes, and interfaces.
Java Objects
Objects are the instances of a class that are created to use the attributes and methods of a
class. A typical Java program creates many objects, which as you know, interact by invoking
methods. An object consists of:
• State: It is represented by attributes of an object. It also reflects the properties
of an object.
• Behaviour: It is represented by the methods of an object. It also reflects the
response of an object with other objects.
• Identity: It gives a unique name to an object and enables one object
to interact with other objects.
Access modifiers:
In Java, access modifiers are essential tools that define how the members of a class,
like variables, methods, and even the class itself, can be accessed from other parts of our
program.
Types of Access Modifiers
There are 4 types of access modifiers available in Java:
1. Default - No keyword required
2. Private
3. Protected
4. Public
1) default:
When no access modifier is specified for a class, method, or data member, it is said to have
the default access modifier by default. This means only classes within the same package can
access it.
2) private:
The private access modifier is specified using the keyword private. The methods or data
members declared as private are accessible only within the class in which they are declared.
• Any other class of the same package will not be able to access these members.
3) protected:
The protected access modifier is specified using the keyword protected. The methods or data
members declared as protected are accessible within the same package or subclasses in
different packages.
4) public:
The public access modifier is specified using the keyword public.
• The public access modifier has the widest scope among all other access modifiers.
• Classes, methods, or data members that are declared as public are accessible
from everywhere in the program. There is no restriction on the scope of public
data members.
Class members:
In Java, "class members" refer to the components defined within a class that represent its
attributes and behaviours. These include:
• Fields (Variables):
These represent the data or state of an object.
• Instance Variables: Declared without the static keyword, these variables
belong to a specific instance (object) of a class. Each object has its own copy
of instance variables.
• Static Variables (Class Variables): Declared with the static keyword, these
variables belong to the class itself, not to any specific instance. There is only
one copy of a static variable, shared by all instances of the class.
• Methods:
These represent the behaviours or actions that an object can perform.
• Instance Methods: Declared without the static keyword, these methods
operate on the instance variables of a specific object. They require an object
to be invoked.
• Static Methods (Class Methods): Declared with the static keyword, these
methods belong to the class and can be called directly using the class name,
without creating an object. They can only access static fields and other static
methods.
• Nested classes:
A class inside another class.
1) non-static nested class
2) static nested class
• Constructors:
In Java, a constructor is a special type of method used to initialize objects when they
are created. It has the same name as the class and does not have a return type, not
even void. Constructors are automatically invoked when an object is instantiated using
the new keyword.
Declaration of class objects in java:
In Java, declaring a class object, also known as creating an instance of a class, involves a two-
step process: declaration and instantiation/initialization.
1. Declaration:
This step involves declaring a variable that will hold a reference to the object. The type of this
variable is the class name.
ClassName objectName;
Here, ClassName is the name of the class, and objectName is the name you choose for your
object variable. At this point, objectName is a reference variable but does not yet point to an
actual object in memory; it holds a null value.
2. Initialization
This step involves creating the actual object in memory and assigning its reference to the
declared variable. This is done using the new keyword followed by a call to the class's
constructor.
objectName = new ClassName();
Assigning One object to another:
In Java, assigning one object to another using the assignment operator (=) results in both object
reference variables pointing to the same object in memory.
MyClass obj1 = new MyClass();
MyClass obj2 = obj1; // obj2 now refers to the same object as obj1
You are not creating a new object. You are simply making obj2 point to the same object in
memory that obj1 is pointing to.
In this scenario, obj1 and obj2 are both reference variables, and after the assignment, they both
hold the memory address of the single MyClass object created with new MyClass(). Any
modifications made to the object through obj1 will be reflected when accessed through obj2,
and vice-versa, because they are interacting with the identical underlying object.
Access control for class members:
Determining the access levels for the class members (fields, methods, nested classes,
constructors).
These are public, private, default, protected.
Accessing private members of class:
In Java, private members (fields and methods) of a class are designed to be accessible only
within that class itself, upholding the principle of encapsulation. However, there are two
primary ways to interact with or access private members from outside the class:
• Through Public Getter and Setter Methods (Encapsulation)
Constructors:
• A constructor is a special method that is called automatically when an object is
created.
• It has the same name as the class.
• It does not have a return type (not even void).
• Used to initialize object fields.
• It is invoked automatically when an object of the class is created using
the new keyword.
Types of constructors:
Primarily, there are three types of constructors. They are:
1) Default constructors
2) parametrized constructors
3) copy constructors
1) Default constructors:
In Java, a default constructor is a no-argument constructor that is automatically provided by
the Java compiler if a class does not explicitly define any constructors.
2) parameterized constructor:
A parameterized constructor in Java is a constructor that accepts one or more parameters. It is
used to initialize the instance variables of an object with specific values at the time of object
creation.
3) Copy constructor:
A copy constructor in Java is a special type of constructor that facilitates the creation of a new
object by copying the values of an existing object of the same class.
Constructor Overloading:
In Java, constructor overloading means having multiple constructors in the same class, each
with different parameter lists (different number or types of parameters).
• To initialize objects in different ways.
• To provide default values when some information is missing.
• To make code more flexible and easier to read.