m1 Notes
m1 Notes
1. Encapsulation:
2. Inheritance:
● Inheritance is a mechanism that allows one class (the child or subclass) to inherit the
attributes and methods of another class (the parent or superclass).
3. Polymorphism:
● It enables a single interface to represent different underlying forms (data types). The
two main types of polymorphism are:
4. Abstraction:
● It allows focusing on what an object does instead of how it does it, simplifying the
interaction with complex systems.
Advantages of OOP
● Modularity: Code is organized into discrete objects, making it easier to manage and
understand.
● Maintainability: Changes in one part of the code can be made with minimal impact on other
parts.
● Flexibility: OOP allows for the creation of flexible systems that can evolve over time.
Primitive data types are the basic types built into Java. There are eight primitive data types:
Data
Type Size Description
16
short bits Integer type, range: -32,768 to 32,767
64
long bits Integer type, range: -2^63 to 2^63-1
32
float bits Floating-point type, for decimal values
64
double bits Floating-point type, for double precision
16
char bits Single 16-bit Unicode character
Reference data types refer to objects and arrays. They store references to the actual data
rather than the data itself.
Variables in Java
A variable is a named storage location in memory that holds data. Variables must be
declared with a specific data type before use.
Control statements in Java allow you to dictate the flow of execution in your program. The
most commonly used control statements are if, switch, and loops like while. Below is an
explanation of each, along with examples.
1. if Statement
The if statement is used to execute a block of code conditionally. If the condition evaluates
to true, the code inside the if block is executed.
Syntax:
java
1if (condition) {
3}
Example:
java
if (number > 0) {
} else {
}}}
2. switch Statement
The switch statement is used to execute one block of code among multiple options based on
the value of a variable. It is often used as an alternative to multiple if statements.
Syntax:
java
switch (expression) {
case value1;
break;
case value2:
break;
default: }
3. while Loop
The while loop is used to execute a block of code repeatedly as long as a specified condition
is true. It checks the condition before each iteration.
Syntax:
java
1while (condition) {
Example:
java
3 int count = 1;
count++;
}}}
Summary
● if Statement: Used for conditional execution of code blocks based on boolean expressions.
● switch Statement: Used for multi-way branching based on the value of a variable.
● while Loop: Used for repeated execution of a block of code as long as a condition is true.