OOPs using Java , easyNotes
OOPs using Java , easyNotes
For example:-
class Car {
String color;
int year;
void displayInfo() {
} }
myCar.color = "Red";
myCar.year = 2020;
myCar.displayInfo();
} }
- Objects can interact with one another, while classes do not interact
directly.
3. Encapsulation
Example Question:
class Student {
this.rollNo = rollNo;
return rollNo;
this.name = name;
return name;
} }
4. Inheritance
Example:
class Animal {
void eat() {
} }
void walk() {
} }
void bark() {
} }
dog.eat();
dog.walk();
dog.bark();
} }
5. Polymorphism
Types of Polymorphism :
1. Compile-Time Polymorphism :
2. Runtime Polymorphism :
Benefits:
- Increases code clarity and usability.
class MathOperations {
return a + b;
return a + b + c;
return a + b;
} }
} }
- What is Method?
- The method in the subclass must have the same name, return type,
and parameters as the method in the superclass.
- Instantiate the subclass and call the overridden method to see the
effect.
Example Question:
-Benefits :
1. Definition :
2. Implementation:
3. Inheritance:
- A class can inherit from only one abstract class (single inheritance).
4. Access Modifiers:
5. Fields :
- Interfaces cannot have instance fields; they can only have static
final constants.
6. Use Cases:
- Abstract classes are used when there is a common base with shared
code and behavior among related classes.
- Interfaces are used to define a contract for classes that may not be
related but need to implement the same methods.
Example:
} }
void makeSound() {
System.out.println("Bark");
} }
dog.makeSound();
dog.eat();
} }
7. Constructors
- Constructors have the same name as the class they belong to and do
not have a return type, not even void.
---
- Constructors are invoked only once when an object is instantiated,
while methods can be called multiple times on an object after it has
been created.
int number;
String text;
// Default constructor
public Example() {
number = 0;
text = "Default";
} }
int number;
String text;
// Parameterized constructor
text = str;
} }
8. Interfaces
```java
interface Animal {
void eat();
}
interface Pet {
void play();
@Override
@Override
}
}
```
In this example, the `Dog` class implements both the `Animal` and
`Pet` interfaces, providing concrete implementations for the `eat` and
`play` methods.
Example:
interface Printable {
void print();
interface Showable {
void show();
System.out.println("Print Method");
System.out.println("Show Method");
} }
obj.print();
obj.show();
} }
9. Access Modifiers
1. Public Access :
2. Read-Only Access :
- Definition : Users can view data but cannot make any changes.
3. Write Access :
- Definition : Users can modify existing data and add new data.
- 4. Admin Access :
5. Restricted Access :
6. Temporary Access :
- Try Block:
- The `try` block contains the code that may potentially throw an
exception.
- Catch Block:
- The `catch` block is used to handle the exception that was thrown in
the `try` block.
- Finally Block :
- The `finally` block is optional and is executed after the `try` and
`catch` blocks, regardless of whether an exception was thrown or
caught.
- The `finally` block ensures that certain code runs no matter what,
providing a reliable way to execute cleanup code.
- Flow of Control:
- Benefits:
Example:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
} finally {
System.out.println("End of program.");
}
What is multithreading, and how does it relate to OOP
concepts in Java?