Reference NOTES
Java OOP (Object Oriented Programming) Concepts
Before Object-Oriented Programming (OOPs), most programs used a procedural approach,
where the focus was on writing step-by-step functions. This made it harder to manage and
reuse code in large applications.
To overcome these limitations, Object-Oriented Programming was introduced. Java is built
around OOPs, which helps in organizing code using classes and objects.
Key Features of OOP in Java:
Structures code into logical units (classes and objects)
Keeps related data and methods together (encapsulation)
Makes code modular, reusable and scalable
Prevents unauthorized access to data
Follows the DRY (Don’t Repeat Yourself) principle
1. 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.
Using classes, you can create multiple objects with the same behavior instead of writing
their code multiple times. In general, class declarations can include these components in
order:
Modifiers: A class can be public or have default access (Refer to this for details).
Class name: The class name should begin with the initial letter capitalized by
convention.
Body: The class body is surrounded by braces, { }.
2. Object
An Object is a basic unit of Object-Oriented Programming that represents real-life entities.
A typical Java program creates many objects, which as you know, interact by invoking
methods. The objects are what perform your code, they are the part of your code visible to
the viewer/user. An object mainly consists of:
State: It is represented by the attributes of an object. It also reflects the properties of an
object.
Behavior: It is represented by the methods of an object. It also reflects
3. Abstraction
Abstraction in Java is the process of hiding the implementation details and only showing
the essential details or features to the user. It allows to focus on what an object does rather
than how it does it. The unnecessary details are not displayed to the user.
4. Encapsulation
Encapsulation is defined as the process of wrapping data and the methods into a single unit,
typically a class. It is the mechanism that binds together the code and the data. It
manipulates. Another way to think about encapsulation is that it is a protective shield that
prevents the data from being accessed by the code outside this shield.
Technically, in encapsulation, the variables or the data in a class is hidden from any
other class and can be accessed only through any member function of the class in which
they are declared.
In encapsulation, the data in a class is hidden from other classes, which is similar to
what data-hiding does. So, the terms "encapsulation" and "data-hiding" are used
interchangeably.
Encapsulation can be achieved by declaring all the variables in a class as private and
writing public methods in the class to set and get the values of the variables.
5. Inheritance
Inheritance is an important pillar of OOP (Object Oriented Programming). It is the
mechanism in Java by which one class is allowed to inherit the features (fields and
methods) of another class. We are achieving inheritance by using extends keyword.
Inheritance is also known as "is-a" relationship.
Example: Dog, Cat, Cow can be Derived Class of Animal Base Class.
6. Polymorphism
The word polymorphism means having many forms, and it comes from the Greek
words poly (many) and morph (forms), this means one entity can take many forms. In
Java, polymorphism allows the same method or object to behave differently based on the
context, specially on the project's actual runtime class.
Method Overloading and Method Overriding
1. Method Overloading: Also, known as compile-time polymorphism, is the concept of
Polymorphism where more than one method share the same name with different signature
(Parameters) in a class. The return type of these methods can or cannot be same.
2. Method Overriding: Also, known as run-time polymorphism, is the concept of
Polymorphism where method in the child class has the same name, return-type and
parameters as in parent class. The child class provides the implementation in the method
already written.
Java Operators
Operators are special symbols that perform operations on variables or values. These
operators are essential in programming as they allow you to manipulate data efficiently.
Types of Operators in Java
1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift Operators
9. An instance of an operator
1. Arithmetic Operators
Arithmetic Operators are used to perform simple arithmetic operations on primitive and
non-primitive data types.
* : Multiplication
/ : Division
% : Modulo
+ : Addition
- : Subtraction
// Java Program to show the use of
// Arithmetic Operators
import [Link].*;
class Test1
{
public static void main (String[] args)
{
int a = 10;
int b = 3;
// Arithmetic operators on Strings
String n1 = "15";
String n2 = "25";
// Convert Strings to integers
int a1 = [Link](n1);
int b1 = [Link](n2);
[Link]("a + b = " + (a + b));
[Link]("a - b = " + (a - b));
[Link]("a * b = " + (a * b));
[Link]("a / b = " + (a / b));
[Link]("a % b = " + (a % b));
[Link]("a1 + b1 = " + (a1 + b1));
}}
2. Unary Operators
Unary Operators need only one operand. They are used to increment, decrement, or negate a
value.
- , Negates the value.
+ , Indicates a positive value (automatically converts byte, char, or short to int).
++ , Increments by 1.
o Post-Increment: Uses value first, then increments.
o Pre-Increment: Increments first, then uses value.
-- , Decrements by 1.
o Post-Decrement: Uses value first, then decrements.
o Pre-Decrement: Decrements first, then uses value.
! , Inverts a boolean value.
// Java Program to show the use of
// Unary Operators
import [Link].*;
// Driver Class
class program {
// main function
public static void main(String[] args)
{
// Interger declared
int a = 10;
int b = 10;
// Using unary operators
[Link]("Postincrement : " + (a++));
[Link]("Preincrement : " + (++a));
[Link]("Postdecrement : " + (b--));
[Link]("Predecrement : " + (--b));
}
}
3. Assignment Operator
'=' The assignment operator is used to assign a value to any variable. It has right-to-left
associativity, i.e. value given on the right-hand side of the operator is assigned to the
variable on the left, and therefore right-hand side value must be declared before using it or
should be a constant.
The general format of the assignment operator is:
variable = value;
In many cases, the assignment operator can be combined with others to create shorthand
compound statements. For example, a += 5 replaces a = a + 5. Common compound
operators include:
+= , Add and assign.
-= , Subtract and assign.
*= , Multiply and assign.
/= , Divide and assign.
%= , Modulo and assign.
// Java Program to show the use of
// Assignment Operators
import [Link].*;
// Driver Class
class program {
// Main Function
public static void main(String[] args)
{
// Assignment operators
int f = 7;
[Link]("f += 3: " + (f += 3));
[Link]("f -= 2: " + (f -= 2));
[Link]("f *= 4: " + (f *= 4));
[Link]("f /= 3: " + (f /= 3));
[Link]("f %= 2: " + (f %= 2));
[Link]("f &= 0b1010: " + (f &= 0b1010));
[Link]("f |= 0b1100: " + (f |= 0b1100));
[Link]("f ^= 0b1010: " + (f ^= 0b1010));
[Link]("f <<= 2: " + (f <<= 2));
[Link]("f >>= 1: " + (f >>= 1));
[Link]("f >>>= 1: " + (f >>>= 1));
}
}
4. Relational Operators
Relational Operators are used to check for relations like equality, greater than, and less than.
They return boolean results after the comparison and are extensively used in looping
statements as well as conditional if-else statements. The general format is ,
variable relation_operator value
Relational operators compare values and return Boolean results:
== , Equal to.
!= , Not equal to.
< , Less than.
<= , Less than or equal to.
> , Greater than.
>= , Greater than or equal to.
// Java Program to show the use of
// Relational Operators
import [Link].*;
// Driver Class
class program {
// main function
public static void main(String[] args)
{
// Comparison operators
int a = 10;
int b = 3;
int c = 5;
[Link]("a > b: " + (a > b));
[Link]("a < b: " + (a < b));
[Link]("a >= b: " + (a >= b));
[Link]("a <= b: " + (a <= b));
[Link]("a == c: " + (a == c));
[Link]("a != c: " + (a != c));
}
}
5. Logical Operators
Logical Operators are used to perform "logical AND" and "logical OR" operations, similar
to AND gate and OR gate in digital electronics. They have a short-circuiting effect, meaning
the second condition is not evaluated if the first is false.
Conditional operators are:
&&, Logical AND: returns true when both conditions are true.
||, Logical OR: returns true if at least one condition is true.
!, Logical NOT: returns true when a condition is false and vice-versa
// Java Program to show the use of
// Logical operators
import [Link].*;
class program {
// Main Function
public static void main (String[] args) {
// Logical operators
boolean x = true;
boolean y = false;
[Link]("x && y: " + (x && y));
[Link]("x || y: " + (x || y));
[Link]("!x: " + (!x));
}
}
6. Ternary operator
The Ternary Operator is a shorthand version of the if-else statement. It has three operands
and hence the name Ternary. The general format is,
condition ? if true : if false
The above statement means that if the condition evaluates to true, then execute the
statements after the '?' else execute the statements after the ':'.
// ternary operator.
public class program {
public static void main(String[] args)
{
int a = 20, b = 10, c = 30, result;
// result holds max of three
// numbers
result = ((a > b) ? (a > c) ? a : c : (b > c) ? b : c);
[Link]("Max of three numbers = "+ result);
}
}
7. Bitwise Operators
Bitwise Operators are used to perform the manipulation of individual bits of a number and
with any of the integer types. They are used when performing update and query operations
of the Binary indexed trees.
& (Bitwise AND): returns bit-by-bit AND of input values.
| (Bitwise OR): returns bit-by-bit OR of input values.
^ (Bitwise XOR): returns bit-by-bit XOR of input values.
~ (Bitwise Complement): inverts all bits (one's complement).
// Java Program to show the use of
// bitwise operators
import [Link].*;
class Programs
{
public static void main(String[] args)
{
// Bitwise operators
int d = 0b1010;
int e = 0b1100;
[Link]("d & e : " + (d & e));
[Link]("d | e : " + (d | e));
[Link]("d ^ e : " + (d ^ e));
[Link]("~d : " + (~d));
[Link]("d << 2 : " + (d << 2));
[Link]("e >> 1 : " + (e >> 1));
[Link]("e >>> 1 : " + (e >>> 1));
}
}
8. Shift Operators
Shift Operators are used to shift the bits of a number left or right, thereby multiplying or
dividing the number by two, respectively. They can be used when we have to multiply or
divide a number by two. The general format ,
number shift_op number_of_places_to_shift;
<< (Left shift): Shifts bits left, filling 0s (multiplies by a power of two).
>> (Signed right shift): Shifts bits right, filling 0s (divides by a power of two), with the
leftmost bit depending on the sign.
>>> (Unsigned right shift): Shifts bits right, filling 0s, with the leftmost bit always 0.
// Java Program to show the use of
// shift operators
import [Link].*;
class Program
{
public static void main(String[] args)
{
int a = 10;
// Using left shift
[Link]("a<<1 : " + (a << 1));
// Using right shift
[Link]("a>>1 : " + (a >> 1));
}
}
9. instanceof Operator
The instanceof operator is used for type checking. It can be used to test if an object is an
instance of a class, a subclass, or an interface. The general format,
object instance of class/subclass/interface
// Java program to show the use of
// Instance of operator
public class Test
{
public static void main(String[] args)
{
Person obj1 = new Person();
Person obj2 = new Boy();
// As obj is of type person, it is not an
// instance of Boy or interface
[Link]("obj1 instanceof Person: "
+ (obj1 instanceof Person));
[Link]("obj1 instanceof Boy: "
+ (obj1 instanceof Boy));
[Link]("obj1 instanceof MyInterface: "
+ (obj1 instanceof MyInterface));
// Since obj2 is of type boy,
// whose parent class is person
// and it implements the interface Myinterface
// it is instance of all of these classes
[Link]("obj2 instanceof Person: "
+ (obj2 instanceof Person));
[Link]("obj2 instanceof Boy: "
+ (obj2 instanceof Boy));
[Link]("obj2 instanceof MyInterface: "
+ (obj2 instanceof MyInterface));
}
}
// Classes and Interfaces used
// are declared here
class Person {
}
class Boy extends Person implements MyInterface {
}
interface MyInterface {
}
The common mistakes that can occur when working with Java Operators are listed below:
Confusing == with =: Using == for assignment instead of = for equality check leads to
logical errors.
Incorrect Use of Floating Point Comparison: Comparing floating point numbers using ==
can lead to unexpected results due to precision issues.
Integer Division Confusion: Dividing two integers will result in integer division
(truncating the result).
Overusing + for String Concatenation in Loops: Using + for concatenating strings in
loops leads to performance issues because it creates new string objects on each iteration.