Java Notes
Java Notes
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.
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.
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:
int count = 1;
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.
int count = 1
do {
count++;
}
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.
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.