CS & IT
ENGINEERING
Java With OOPs
OOPs using Java
Lecture No-01 By- Aditya sir
Recap of Previous Lecture
Topic Collection
Framework
Topics to be Covered
Topic Java Collections
OOP
3
Topic : What is OOP?
Definition:
○ OOP (Object-Oriented Programming) is a programming paradigm based on
the concept of "objects" that encapsulate data and behavior.
• Key Principles:
○ Encapsulation: Bundling data (attributes) and methods (functions) that
operate on the data into a single unit (class).
○ Inheritance: Ability to create new classes based on existing classes,
inheriting attributes and behaviors.
○ Polymorphism: Ability to treat objects of different classes through a
common interface, typically using method overriding or overloading.
○ Abstraction: Hiding complex implementation details and showing only the
necessary features of an object.
Topic : What is OOP?
• Benefits:
○ Modularity, reusability, ease of maintenance, and improved problem
modeling.
Topic : Classes and Objects
• Classes:
○ Blueprint for creating objects.
○ Contains attributes (data members) and methods (functions) that define
the object's behavior.
• Objects:
○ Instances of a class created at runtime.
• Constructors:
○ Special methods that initialize new objects.
Topic : Classes and Objects
• Access Modifiers:
○ public, private, protected – control access to class members.
• Static vs. Instance Members:
○ Static members: Belong to the class; shared across all instances.
○ Instance members: Unique to each object instance.
Topic : Code Examples on Classes and Objects
Example 1 – Basic Class with Attributes and Methods
• Problem Statement: Create a simple class Animal with attributes and a method
to display details.
• Hint: Define instance variables and a method to print the object's data.
Topic : Code Examples on Classes and Objects
• Code:
public class SillyAnimal {
// Instance variables
String type;
int age;
// Method to display details
public void displayInfo() {
[Link]("Type: " + type + ", Age: " + age);
}
public static void main(String[] args) {
so
// Creating an object of SillyAnimal
SillyAnimal a = new SillyAnimal();
[Link] = "Cat";
[Link] = 3;
[Link]();
}
}
Topic : Code Examples on Classes and Objects
• Expected Output:
Type: Cat, Age: 3
Topic : Code Examples on Classes and Objects
Example 2 – Using Constructors
• Problem Statement: Create a class Vehicle that initializes its attributes using a
constructor.
• Hint: Define a constructor that accepts parameters to initialize object
properties.
Topic : Code Examples on Classes and Objects
• Code:
public class WackyVehicle {
String brand;
int year;
// Constructor with parameters
public WackyVehicle(String brand, int year) {
TJ
[Link] = brand;
[Link] = year;
}
public void showInfo() {
[Link]("Brand: " + brand + ", Year: " + year);
}
public static void main(String[] args) {
WackyVehicle car = new WackyVehicle("Toyota", 2020);
[Link]();
}
}
Topic : Code Examples on Classes and Objects
• Expected Output:
Brand: Toyota, Year: 2020
Topic : Code Examples on Classes and Objects
Example 3 – Encapsulation with Getters and Setters
• Problem Statement: Create a class Student with private attributes and provide
public getters and setters.
• Hint: Use private variables and define public methods to access and modify
them.
private within class
only
public anywhere
Topic : Code Examples on Classes and Objects
• Code:
public class CrazyStudent {
// Private attributes for encapsulation
private String name;
private int marks;
// Constructor
public CrazyStudent(String name, int marks) {
[Link] = name;
[Link] = marks;
}
// Getter and Setter for name
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
Topic : Code Examples on Classes and Objects
// Getter and Setter for marks
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
[Link] = marks;
}
public void display() {
[Link]("Student: " + name + ", Marks: " + marks);
}
public static void main(String[] args) {
CrazyStudent s = new CrazyStudent("John", 85);
[Link]();
[Link](90);
[Link]();
}
}
Topic : Code Examples on Classes and Objects
• Expected Output:
Student: John, Marks: 85
Student: John, Marks: 90
Topic : Code Examples on Classes and Objects
Example 4 – Static vs. Instance Members
• Problem Statement: Create a class Counter that tracks the number of instances
created.
• Hint: Use a static variable to count objects.
Topic : Code Examples on Classes and Objects
• Code:
public class LoonyCounter {
// Static variable to count objects
private static int count = 0; instance
// Instance variable
private String id;
// Constructor increments count and sets id
public LoonyCounter(String id) {
Ite
[Link] = id;
count++;
}
public static int getCount() {
return count;
}
Topic : Code Examples on Classes and Objects
Show infor
[Link]("Object " + id + " created.");
} a Al
public static void main(String[] args) {
LoonyCounter a = new LoonyCounter("A1"); b BI
LoonyCounter b = new LoonyCounter("B1");
[Link](); count 1
[Link]();
[Link]("Total objects: " + [Link]());
} ttp
}
Topic : Code Examples on Classes and Objects
• Expected Output:
Object A1 created.
Object B1 created.
Total objects: 2
Topic : Code Examples on Classes and Objects
• Expected Output:
Balance: $150.0
Topic : Code Examples on Classes and Objects
Example 5 – Constructor Overloading
• Problem Statement: Create a class Book with overloaded constructors to
initialize objects in different ways.
• Hint: Define multiple constructors with different parameter lists.
Topic : Code Examples on Classes and Objects
Example 6 – Creating and Using Objects
• Problem Statement: Create a class Calculator with methods for addition and
subtraction; then create objects and use them.
• Hint: Define instance methods and call them on objects.
Topic : Code Examples on Classes and Objects
• Code:
public class SillyCalculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public static void main(String[] args) {
SillyCalculator calc = new SillyCalculator();
HE
[Link]("Addition: " + [Link](10, 5));
[Link]("Subtraction: " + [Link](10, 5));
}
}
Topic : Code Examples on Classes and Objects
• Expected Output:
Addition: 15
Subtraction: 5
THANK - YOU