[go: up one dir, main page]

0% found this document useful (0 votes)
18 views9 pages

Untitled Document

Untitled document

Uploaded by

chormeosflex1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

Untitled Document

Untitled document

Uploaded by

chormeosflex1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Here’s a combined and comprehensive set of Java OOP notes that covers all major topics and

subtopics in a structured format. You can directly use this for your competitive exam
preparation.

Java Object-Oriented Programming (OOP)


Notes

1. Object-Oriented Programming (OOP) Basics


● Class: A blueprint or template for creating objects. It defines fields (attributes) and
methods (behaviors).
● Object: An instance of a class. Represents an entity with a state (data) and behavior
(methods).
● Constructor: A special method used to initialize objects.
○ Default Constructor: No arguments, automatically created if no constructor is
defined.
○ Parameterized Constructor: Takes arguments to initialize specific fields.
○ Copy Constructor: Used to create a new object by copying fields of another
object.

Example:
java
Copy code
class Car {
String model;
int year;
Car(String model, int year) {
this.model = model;
this.year = year;
}
}


2. Encapsulation
● Definition: The process of bundling data (fields) and methods that operate on the data
into a single unit (class) while restricting direct access to some components using private
access modifiers.

Purpose: Protects the integrity of an object's state and provides controlled access through
public methods (getters and setters).
Example:
java
Copy code
class BankAccount {
private double balance;

public double getBalance() {


return balance;
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
}
}
}

3. Inheritance
● Definition: Allows a class (child/subclass) to inherit fields and methods from another
class (parent/superclass).
● Why use it? It promotes code reuse and establishes a natural hierarchy.
● Types of Inheritance:
1. Single Inheritance: One class inherits from one parent class.
2. Multilevel Inheritance: A class inherits from a class, which also inherits from
another class.
3. Hierarchical Inheritance: Multiple classes inherit from the same parent class.
4. Hybrid Inheritance: Combination of different types of inheritance, achieved
using interfaces (Java doesn't support multiple inheritance directly).
Example:
java
Copy code
class Animal {
void eat() {
System.out.println("This animal eats");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Dog barks");
}
}

4. Polymorphism
● Definition: Polymorphism allows one entity (method or object) to take many forms.
● Types of Polymorphism:
1. Compile-time Polymorphism (Method Overloading):
■ Same method name but different parameters.
■ Enhances flexibility and code readability.

Example:
java
Copy code
class MathOperations {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}

2.
3. Runtime Polymorphism (Method Overriding):
■ A child class provides its own implementation of a method defined in its
parent class.
■ Achieved through inheritance.

Example:
java
Copy code
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}

4.

5. Abstraction
● Definition: Hides implementation details and shows only necessary features.
● Achieved through:
1. Abstract Classes: Cannot be instantiated; can have both abstract and concrete
methods.
2. Interfaces: All methods are abstract by default. Allows multiple inheritance.

Abstract Class Example:


java
Copy code
abstract class Animal {
abstract void sound();
void sleep() {
System.out.println("Animal is sleeping");
}
}

class Dog extends Animal {


void sound() {
System.out.println("Dog barks");
}
}
Interface Example:
java
Copy code
interface Vehicle {
void start();
}

class Car implements Vehicle {


public void start() {
System.out.println("Car starts");
}
}

6. Static vs Non-static Members


● Static Members: Belong to the class, not instances. They can be accessed without
creating an object.

Non-static Members: Belong to individual objects of the class.


Example:
java
Copy code
class MathUtil {
static int counter = 0;
static void incrementCounter() {
counter++;
}
}


7. Final Keyword
● Final Variable: Cannot be reassigned (constant).
● Final Method: Cannot be overridden by subclasses.

Final Class: Cannot be subclassed.


Example:
java
Copy code
final class Vehicle { }

class Car extends Vehicle { } // Error: cannot extend final class

8. This Keyword
● Refers to the current object within a method or constructor.

Used to distinguish between instance variables and method parameters with the same name.
Example:
java
Copy code
class Car {
String model;
Car(String model) {
this.model = model; // 'this' refers to instance variable
}
}

9. Super Keyword
● Refers to the parent class object.

Used to access parent class methods, fields, or constructors.


Example:
java
Copy code
class Animal {
String color = "White";
}

class Dog extends Animal {


String color = "Black";

void printColor() {
System.out.println(color); // prints Black
System.out.println(super.color); // prints White
}
}

10. Access Modifiers


● Private: Accessible only within the same class.
● Default: Accessible within the same package.
● Protected: Accessible within the same package and subclasses.

Public: Accessible from anywhere.


Example:
java
Copy code
class Example {
private int x = 10;
protected int y = 20;
public int z = 30;
}

11. Association, Aggregation, and Composition


● Association: A general relationship where classes interact with each other.

Aggregation (Has-a relationship): One class contains another, but both can exist
independently (weak association).
Example:
java
Copy code
class Car {
Engine engine; // Car has an Engine
}

Composition: Strong form of aggregation where one class owns another and both are
dependent. If the parent is destroyed, so is the child.
Example:
java
Copy code
class House {
Room room; // House owns Room
}

12. Exception Handling


● try block: Contains code that may throw an exception.
● catch block: Handles the exception.

finally block: Executes regardless of whether an exception occurred.


Example:
java
Copy code
try {
int data = 100 / 0;
} catch (ArithmeticException e) {
System.out.println("Division by zero error!");
} finally {
System.out.println("This block always executes");
}

These notes should cover all the essential OOP concepts in Java that you need for your
competitive exam. They include clear explanations of each topic, with examples to help you
understand the concepts. Let me know if you need any further clarification or adjustments!

You might also like