0% found this document useful (0 votes)
139 views

Java Notes

The document provides an overview of object-oriented programming concepts and Java programming language fundamentals. It defines OOP concepts like encapsulation, inheritance, polymorphism, and abstraction. It then explains that Java is an object-oriented, platform-independent language and describes some of its key features. The document also defines classes, objects, decision-making statements like if-else, loops like for, while, do-while, constructors, and functions in Java.

Uploaded by

aliroomanq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

Java Notes

The document provides an overview of object-oriented programming concepts and Java programming language fundamentals. It defines OOP concepts like encapsulation, inheritance, polymorphism, and abstraction. It then explains that Java is an object-oriented, platform-independent language and describes some of its key features. The document also defines classes, objects, decision-making statements like if-else, loops like for, while, do-while, constructors, and functions in Java.

Uploaded by

aliroomanq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Object-Oriented Programming NOTES

What is Object-Oriented Programming?


It's a way of writing computer programs that focuses on creating "objects" that interact with
each other to perform tasks. Here are the four main concepts of OOP explained in simple terms:

OOP four concepts.

Encapsulation:
This is like putting something in a box and giving it a label. In programming, it means bundling
the data (variables) and methods (functions) that operate on the data into a single unit, an
"object." This helps keep the data safe from outside interference and makes it easier to manage.

Inheritance:
Think of inheritance as passing down traits from parents to children. In OOP, it means that
objects can inherit characteristics (data and behavior) from other objects. This helps in creating
a hierarchical relationship between classes (blueprints for objects), allowing for code reuse and
the creation of more specialized classes.

Polymorphism:
This is like a person having different roles in different situations. In programming, polymorphism
allows objects of different classes to be treated as objects of a common superclass. This means
that a single interface (method name) can be used to manipulate objects of various types,
making the code more flexible and adaptable.

Abstraction:
Abstraction is like using a remote control without needing to know how it works internally. In
OOP, it means hiding the complex inner workings of objects and only showing the necessary
features. This helps in managing complexity by focusing on what an object does rather than
how it does it, making the code easier to understand and maintain.
Object-Oriented Programming NOTES

What is java?
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned
by Oracle Corporation) in the mid-1990s. It is designed to be platform-independent, meaning that Java
programs can run on any device or operating system that has a Java Virtual Machine (JVM) installed. This
feature is achieved through the concept of "write once, run anywhere" (WORA).

Java is known for its simplicity, readability, and robustness. It is widely used for developing a variety of
applications, including web applications, mobile apps, enterprise software, and large-scale systems.

Some key features of Java include:


Object-oriented: Java follows the principles of object-oriented programming, allowing developers to
create modular and reusable code using classes and objects.

Platform independence: Java code is compiled into bytecode, which can be executed on any device with
a compatible JVM. This makes Java programs highly portable.

Automatic memory management: Java uses a garbage collector to automatically manage memory
allocation and deallocation, reducing the risk of memory leaks and improving the overall stability of Java
applications.

Rich standard library: Java comes with a comprehensive standard library (Java API) that provides ready-
to-use classes and methods for various common tasks, such as networking, file I/O, and database access.

What is class in java?


Class in Java is like a blueprint or a recipe for creating objects. It defines what an object will look
like and what it can do. Think of it as a template that describes the properties (variables) and
actions (methods) that each object of that class will have. For example, if you have a class called
"Car," it would describe the features of all cars, such as color, speed, and the ability to drive. You
can then create specific instances of this class, like a red sports car or a blue sedan, each with its
own unique properties but following the same general blueprint.
Object-Oriented Programming NOTES
What is the object in java?
Object is a specific instance of a class, embodying the properties and behaviors defined by that
class. It represents a real-world entity or concept and can interact with other objects and
perform tasks based on its defined capabilities. Objects encapsulate data and functionality,
allowing for structured and modular programming.

What is Decision making statements in java?

In Java, decision-making statements allow the program to make choices based on certain
conditions. The most common ones are "if" statements, which execute a block of code if a
condition is true, and "else" statements, which execute a block of code if the condition is false.
These statements help control the flow of the program, allowing it to respond dynamically to
different situations.

Code:
Object-Oriented Programming NOTES
Types of decision-making statement:

What is Loop in java?


In Java, a loop is a programming structure that allows you to repeat a block of code multiple
times based on a condition or a set number of iterations, enabling efficient repetition and
automation in your code.

There are three main types of loops:


for loop:
Executes a block of code a specified number of times, based on a control variable that changes
with each iteration.
Object-Oriented Programming NOTES
while loop:
Java while loop evaluates an expression, which must return the Boolean value. If the expression
evaluates to a true Boolean value, the while statement executes a statement(s) in a while block.
The while statement continues testing the expression and executing its block until that
expression evaluates too false.

public class WhileLoopExample {

public static void main(String[] args) {

int count = 1;

while (count <= 5) {

System.out.println("Count is: " + count);

count++;

}
Object-Oriented Programming NOTES
Do-while loop:
Java do-while loop is used to iterate the part of a program several times. If several iterations are
not fixed and you must execute a loop at least once even if the expression condition is false
because that code block is executed before a condition is tested. If that is the scenario then it is
recommended to use the do-while loop.

public class DoWhileLoopExample {

public static void main(String[] args) {

int count = 1

do {

System.out.println("Count is: " + count);

count++;

} while (count <= 5);

}
Object-Oriented Programming NOTES
What is Constructor in java?
Constructor is a special type of method that is automatically called when an object of a class is
created. Its main purpose is to initialize the newly created object, often by assigning initial
values to its member variables. Constructors have the same name as the class and can be used
to perform setup tasks such as allocating memory or initializing resources needed by the object.

Type of Constructor in java?

Default Constructor:
This constructor is automatically provided by Java if no other constructor is explicitly defined in
a class. It doesn't accept any parameters and initializes the object with default values (e.g., 0 for
numeric types, null for reference types).

Parameterized Constructor:
This constructor accepts one or more parameters, allowing you to initialize the object with
specific values provided as arguments when the object is created. It's useful for customizing the
initial state of objects based on user-defined values.
Object-Oriented Programming NOTES
What is function in java?
function is a named block of code that performs a specific task. It's like a recipe that you can use
whenever you need to perform a particular action. Functions can take input values (called
parameters), perform operations, and optionally return a result. They help make your code
organized, reusable, and easier to understand by breaking it into smaller, manageable parts.

public class FunctionExample {


public static int calculateSum(int num1, int num2) {
int sum = num1 + num2;
return sum;
}

public static void main(String[] args) {


int result = calculateSum(5, 3);

System.out.println("The sum is: " + result);


}
}

You might also like