JAVA OOP’S CONCEPT
Object-Oriented Programming is a paradigm that provides many
concepts, such as inheritance, data binding, polymorphism, etc.
Simula is considered the first object-oriented
programming language.
popular object-oriented languages
are Java, C#, PHP, Python, C++, etc.
Object means a real-world entity such as a pen,
Inheritance
When one object acquires all the properties and behaviors
of a parent object, it is known as inheritance.
Polymorphism
If one task is performed in different ways, it is known as
polymorphism.
Types of Polymorphism:There are two types of polymorphism in Java:
1. Compile-time Polymorphism: method overloading is used to
achieve compile-time polymorphism. A class can have
numerous methods with the same name but distinct
parameter lists thanks to method overloading.
2. Runtime Polymorphism.: Runtime polymorphism or Dynamic Method
Dispatch is a process in which a call to an overridden method is resolved
at runtime rather than compile-time.
If the reference variable of Parent class refers to the
object of Child class, it is known as upcasting.
Abstraction
Hiding internal implementation and showing
functionality only to the user is known as abstraction.
A class that is declared with the abstract keyword is known as
an abstract class in Java. It can have abstract and non-abstract
methods (method with the body).
There are two ways to achieve abstraction in Java:
Using Abstract Class (0 to 100%)
Using Interface (100%)
o An abstract class must be declared with an abstract
keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not
to change the body of the method.
SYNTAX OF ABSTRACT CLASS
1. public abstract class Shape {
2. public abstract double area();
3. public void display() {
4. System.out.println("This is a shape.");
5. }
6. }
Encapsulation
Binding (or wrapping) code and data together into a single
unit are known as encapsulation.
Data members of the class private. Now we can use
setter and getter methods to set and get the data in it.
Encapsulation restricts direct access to fields of a class,
preserving the integrity of data by allowing controlled
access through getter and setter methods.
It increases code complexity due to the getter and setter
methods for every field. It leads to longer code.
It decreases performance by invoking the getter and
setter methods instead of directly accessing variables.
Packages in java: A Java package is a group of similar
types of classes, interfaces and sub-package.
Packages are used to avoid naming conflicts among class
names.
For organizing the class, interfaces and other components,
packages are used.
o Java enhances the reusability management of code and
does the promotion of data encapsulation with
the help of packages.
o Java packages should be in lowercase, and the dot should
separate the components (.). If the component is a class,
then the first letter of the class name should be
capitalized.
o The names of the directories and the package names are
closely related.
o We can access classes of a package using the import
keyword.
1. Built-in Packages:java.lang: It contains those classes that
are required for language support,
java.io: It contains all those classes that are required for
the input/output operations.
java.util: It contains utility classes that are needed for
the data structures, such as Array, List, Map, etc.
java.applet: All those classes that are required for finding
applets are found in this package.
java.awt: It contains all those classes that are required
for creating components of the GUI (Graphical User
Interface).
java.net: It includes classes that are used for doing tasks
in the field of networking.
User-defined Packages: The packages which are declared
by the user itself.
System" is a class present in the java.lang package, and
out is a static variable present in the System class.
Access modifiers in java: There are two types of modifiers
in Java: access modifiers and non-access modifiers.
non-access modifiers, such as static, abstract,
synchronized, native, volatile, transient
o Access modifiers: Public: Used for methods and classes
that need to be globally accessible.
o Protected: Accessible outside the package, but only
through inheritance.
o private: Essential for encapsulation and protecting
sensitive data.
o Default: Limited access only to other classes within the
same package.
Acces Wit Withi Outsi Outsi
s hin n de de
Modifi Clas Pack Pack Pack
er s age age age
by
Subcl
ass
Only
Privat Y N N N
e
Defaul Y Y N N
t
Protec Y Y Y N
ted
Public Y Y Y Y
Java is a high-level, robust, object-oriented and secure
programming language.
Java was developed by Sun Microsystems (which is now a
subsidiary of Oracle) in the year 1995. James Gosling is
known as the father of Java.
public class Main{
public static void main(String args[]){
System.out.println("Hello, World!");
}
}
Types of Java Applications
Standalone Application
Web Application
Enterprise Application
Mobile Application
JVM(JAVA VIRTUAL MACHINE)
an abstract machine. It is a specification that provides a runtime
environment in which Java bytecode can be executed.
VARIABLE IN JAVA
A variable is a container which holds the value while the Java program is
executed. A variable is assigned with a data type.
Two types of data types in Java: primitive and non-primitive. "vary +
able" which means its value can be changed.
local variable: A variable declared inside the body of the
method is called local variable.
instance variable: A variable declared inside the class but
outside the body of the method, is called an instance
variable.
static variable: A variable that is declared as static is
called a static variable. It cannot be local.
Arrays in java: An array is typically a grouping of elements of the same
kind that are stored in a single, contiguous block of memory.
An object of a dynamically formed class is called an array in Java.
Single Dimensional Array: A single-dimensional array in Java is a linear
collection of elements of the same data type.
The Java for-each loop prints the array elements one by one. It
holds an array element in a variable, then executes the body of
the loop.
The syntax of the for-each loop is given below:
for(data_type variable:array){
//body of the loop
}
We can pass the Java array to the method so that we can reuse the same logic
on any array. When we pass an array to a method in Java, we are essentially
passing a reference to the array.
Multidimensional Array: Multi-dimensional arrays in Java are basically
arrays of arrays. It allows us to store data in a tabular or grid-like
structure,
Two-Dimensional (2D) Array
It is the simplest multi-dimensional array having rows and
columns.
DataType[][] arrayName;
arrayName = new DataType[rows][columns];
datatype[][] arrayName = new datatype[rows][columns];
1. Mathematical computations: Representing matrices for algebraic
calculations.
2. Game development: Storing board layouts, game grids, or levels.
3. Image processing: Representing pixels in a digital image.
4. Data analysis: Managing structured data like spreadsheets or databases.
In Java, the array length is the number of elements that an array can
hold. There is no predefined method to obtain the length of an array.
The length property can be invoked by using the dot (.)
operator followed by the array name.
int[] arr=new int[5];
int arrayLength=arr.length();
Jagged Array In Java: A jagged array in Java is a collection of arrays
where each array may contain a varied number of elements.
datatype[][] arrayName = new datatype[numRows][];
arrayName[0] = new datatype[numColumns1];
arrayName[1] = new datatype[numColumns2];
arrayName[numRows-1] = new datatype[numColumnsN];
OPERATORS IN JAVA
Java
programming
Unary operator: The Java unary operators require only one
operand.
Arithimatic Operator: Java arithmetic operators are used to
perform addition, subtraction, multiplication, division, and
modulo operation. They act as basic mathematical operations.
Shift Operator: Java shift operator works on the bits of the
data. It shifts the bits of the number from left to right or right to
left. Left Shift(Signed)(<<) ,Right Shift(unsigned)(>>),
Unsigned Right Shift Operator(>>>)
Java Relational Operators
Java relational or conditional operators are used to check
relationship between two operands such as less than, less than
or equal to, greater than, greater than or equal to, equal to and
not equal to.
< Less Than If the value of the first opera
lesser than the value of the s
operand, the Less Than opera
returns true, otherwise false.
> Greater Than If the value of the first opera
greater than the value of the
operand, the Greater Than o
returns true, otherwise false.
<= Less Than or Equal to When the value of the first o
lesser than or equal to the va
the second operand, the Less
Equal to operator returns tru
otherwise false.
>= Greater Than or Equal to When the value of the first o
greater than or equal to the v
the second operand, the Gre
or Equal to operator returns
otherwise false.
== Equal to Equal to operator checks whe
given operands are equal or n
they are equal, they return tr
otherwise false.
!= Not Equal to Not Equal to operator works
opposite to Equal to operato
returns false if the operands
in value, otherwise true.