UNIT 1 Object Oriented Programming
UNIT 1 Object Oriented Programming
Programming
UNIT I
OOP Definition
● Classes
● Objects
● Encapsulation
● Inheritance
● Polymorphism
● Abstraction
● Composition
OOP Features
Object-Oriented Programming (OOP) has several key features:
1. Encapsulation: Bundles data (attributes) and methods (functions) into a single unit called a class,
restricting direct access to some of the object's components. This helps protect data integrity and
hide implementation details.
2. Inheritance: Allows a new class (subclass) to inherit properties and methods from an existing
class (superclass). This promotes code reuse and establishes a hierarchical relationship between
classes.
3. Polymorphism: Enables objects to be treated as instances of their parent class rather than their
actual class. Methods can be overridden or overloaded to perform different tasks based on the
object's type.
4. Abstraction: Simplifies complex systems by exposing only the necessary and relevant parts of an
object. It allows users to interact with an object through a simplified interface, hiding internal
complexities.
5. Classes and Objects: Classes define the blueprint of objects, including their attributes and
behaviors. Objects are instances of classes, representing specific implementations of the class's
blueprint.
OOP Definition
Core Concepts:
● Classes:
Blueprints for creating objects, defining their properties (attributes) and behaviors (methods).
● Objects:
Instances of classes that encapsulate data and provide methods to manipulate that data.
OOP Concepts
Core Concepts:
● Encapsulation: Bundling data and methods that operate on the data into a single unit (class) and
restricting direct access to some of the object’s components.
OOP Concepts
Core Concepts:
● Inheritance: Mechanism by which a new class (subclass) derives attributes and methods from an
existing class (superclass), promoting code reuse.
OOP Concepts
Core Concepts:
● Polymorphism: Ability to treat objects of different classes through a common interface, allowing
for method overriding and method overloading.
OOP Concepts
Core Concepts:
● Abstraction: Abstraction is the concept of hiding the complex implementation details and showing
only the essential features of an object. It helps to reduce complexity and allows the programmer
to focus on interactions at a higher level without needing to understand all the intricacies
Example: When you drive a car, you don’t need to know how the engine works internally; you just
need to know how to use the steering wheel, accelerator, and brake.
.
OOP Concepts
Core Concepts:
● Composition: Ability to treat objects of different classes through a common interface, allowing for
method overriding and method overloading.
Example: A Car class might be composed of objects like Engine, Wheels, and Seats, each of
which could be an instance of other classes.
JAVA
JAVA Buzzwords
JAVA
1. Exit a Method: The return statement ends the execution of a method and returns control to the caller.
2. Return a Value: For methods that are designed to compute and return a value (non-void methods), the return statement
specifies the value to be returned.
CLASS
Creating Object in java
// Define the Person class
class Person {
// Data members // Main method
String aadharNo; public static void main(String[] args) {
String name; // Creating objects of the Person class
int age; Person john = new Person("1234-5678-9012", "John", 35, "Delhi", "Mal
String city; Person dessy = new Person("5678-9012-3456", "Dessy", 20, "Pune", "M
String gender;
// Displaying details of each person
// Constructor to initialize the Person object john.display();
public Person(String aadharNo, String name, int age, String city, String dessy.display();
gender) {
this.aadharNo = aadharNo; // Invoking methods
this.name = name; john.study();
this.age = age; dessy.play();
this.city = city; }
this.gender = gender; }
}
// Method to represent studying action
public void study() {
System.out.println(name + " is studying.");
}
// Method to represent playing action
public void play() {
System.out.println(name + " is playing.");
}
// Method to display Person details
public void display() {
System.out.println("Aadhar No: " + aadharNo);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
System.out.println("Gender: " + gender);
System.out.println("------------------------");
}
CONTRUCTORS
CONSTRUCTORS types
● DEFAULT
● NO ARGS
● PARAMETERIZED
CONSTRUCTORS types
● DEFAULT
PARAMETERIZED CONSTRUCTOR
METHODS
In Java, a method is a block of code that performs a specific task or
operation. Methods are used to define the behavior of objects and provide
reusable code, making programs more modular and easier to understand and
maintain.
User-defined Methods
METHODS
METHODS
METHODS
1. Built-in Methods:
● These are methods provided by Java's standard library (Java API).
● Examples include methods from classes such as String, Math, System, etc.
● Example:
○ System.out.println(): Prints a message to the console.
○ Math.max(5, 10): Returns the maximum of two numbers.
METHODS
User-defined Methods:
● These are methods defined by the programmer to perform specific tasks.
1. Instance Methods
2. Static Methods
3. Abstract Methods
4. Final Methods
5. Synchronized Methods
6. Native Methods
METHODS
1. Instance Methods:
● Definition: Methods that require an object of the class to be created before they can be called.
● Characteristics:
○ Can access instance variables of the class.
○ Do not have the static keyword.
● Example:
METHODS
Static Methods:
METHODS
Static Methods:
METHODS
Static Methods:
ACCESS MODIFIERS
overview of Java access modifiers:
1. public: Visible everywhere.
public class Example { public int value = 5; }
2. protected: Visible within the same package and subclasses.
class Example { protected int value = 5; }
3. default (no modifier): Visible within the same package.
class Example { int value = 5; // default access }
4. private: Visible only within the same class.
class Example { private int value = 5; }
ACCESS MODIFIERS
ACCESS MODIFIERS
STATIC MEMBERS
3 types
1. Static Variables.
2. Static Method
3. Static Blocks
STATIC MEMBERS
STATIC MEMBERS
STATIC MEMBERS
JAVA COMMENTS
JAVA COMMENTS
JAVA COMMENTS
JAVA COMMENTS
○ @param: Describes method
parameters.
○ @return: Describes the return value.
○ @throws: Describes exceptions that
might be thrown.