Learning Objectives
Understand OOP Concepts
Learn fundamental Object-Oriented Programming principles
Create Classes & Objects
Master class definition and object creation in Java
Constructors & Methods
Implement constructors, methods, and access modifiers
Encapsulation
Apply encapsulation principles and data hiding
Introduction to Object-Oriented Programming
What is OOP?
Object-Oriented Programming is a programming paradigm that organizes code into objects that contain both data (attributes) and code (methods). This approach makes programs more modular, reusable, and easier to maintain.
OOP vs Procedural Programming (C)
C (Procedural)
Focuses on functions and procedures that operate on data
Java (OOP)
Focuses on objects that contain both data and methods
Classes and Objects
Class Definition
A class is a blueprint or template for creating objects. It defines the structure and behavior that objects of that class will have.
public class Car {
// Attributes (instance variables)
private String brand;
private String model;
private int year;
private double price;
// Methods will go here
}
Object Creation
An object is an instance of a class. You create objects using the new
keyword.
// Creating objects
Car myCar = new Car();
Car anotherCar = new Car();
Instance Variables vs Local Variables
- Instance Variables: Belong to the object, declared at class level
- Local Variables: Declared inside methods, only accessible within that method
public class Example {
private int instanceVar; // Instance variable
public void method() {
int localVar = 10; // Local variable
System.out.println(instanceVar); // Can access instance variable
System.out.println(localVar); // Can access local variable
}
public void anotherMethod() {
// System.out.println(localVar); // Error! localVar not accessible here
System.out.println(instanceVar); // Can access instance variable
}
}
Constructors
What are Constructors?
Constructors are special methods that are called when an object is created. They initialize the object's attributes.
Default Constructor
If you don't define any constructors, Java provides a default constructor that initializes numeric types to 0, boolean to false, and objects to null.
public class Student {
private String name;
private int age;
// Default constructor (automatically provided)
// public Student() { }
}
Custom Constructors
You can define your own constructors to initialize objects with specific values.
public class Student {
private String name;
private int age;
// Custom constructor
public Student(String studentName, int studentAge) {
name = studentName;
age = studentAge;
}
// Another constructor (constructor overloading)
public Student(String studentName) {
name = studentName;
age = 18; // Default age
}
}
Methods
Method Definition
Methods define the behavior of objects. They can access and modify the object's attributes.
public class Calculator {
private double result;
// Method to add numbers
public void add(double number) {
result += number;
}
// Method to get result
public double getResult() {
return result;
}
// Method with parameters and return value
public double multiply(double a, double b) {
return a * b;
}
}
Method Types
Instance Methods
Operate on instance variables
Static Methods
Belong to the class, not objects
Void Methods
Don't return a value
Return Methods
Return a value
public class MathUtils {
// Instance method
public void increment() {
count++;
}
// Static method
public static int max(int a, int b) {
return (a > b) ? a : b;
}
// Method with return value
public int getCount() {
return count;
}
}
Method Overloading
Java allows multiple methods with the same name but different parameters.
public class Printer {
public void print(String message) {
System.out.println("String: " + message);
}
public void print(int number) {
System.out.println("Integer: " + number);
}
public void print(double number) {
System.out.println("Double: " + number);
}
}
Access Modifiers
Public vs Private
Access modifiers control how classes, methods, and variables can be accessed.
public class BankAccount {
private double balance; // Only accessible within this class
public String accountNumber; // Accessible from anywhere
public void deposit(double amount) { // Public method
if (amount > 0) {
balance += amount;
}
}
private void validateAmount(double amount) { // Private method
if (amount < 0) {
throw new IllegalArgumentException("Amount cannot be negative");
}
}
}
Access Levels
Practical Examples
Student Management System
public class Student {
// Private instance variables
private String studentId;
private String name;
private int age;
private double gpa;
// Constructor
public Student(String studentId, String name, int age) {
this.studentId = studentId;
this.name = name;
this.age = age;
this.gpa = 0.0;
}
// Getters (accessor methods)
public String getStudentId() { return studentId; }
public String getName() { return name; }
public int getAge() { return age; }
public double getGpa() { return gpa; }
// Setters (mutator methods)
public void setName(String name) { this.name = name; }
public void setAge(int age) {
if (age >= 0) {
this.age = age;
}
}
// Business logic methods
public void updateGpa(double newGpa) {
if (newGpa >= 0.0 && newGpa <= 4.0) {
this.gpa = newGpa;
}
}
public boolean isHonorStudent() {
return gpa >= 3.5;
}
public String getStatus() {
if (gpa >= 3.5) return "Honor Student";
else if (gpa >= 2.0) return "Good Standing";
else return "Academic Warning";
}
// Display method
public void displayInfo() {
System.out.println("Student ID: " + studentId);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("GPA: " + gpa);
System.out.println("Status: " + getStatus());
}
}
Key Differences from C
Concept | C | Java |
---|---|---|
Data Structure | Struct (data only) | Class (data + methods) |
Functions | Separate from data | Part of the class |
Access Control | No built-in protection | Public, private, protected |
Memory Management | Manual (malloc/free) | Automatic (garbage collection) |
Object Creation | Manual allocation | new keyword |
Method Calls | Function calls | Object.method() calls |
Summary
In this module, you've learned:
- ? How to create classes and objects in Java
- ? The role of constructors in object initialization
- ? How to define methods with different access levels
- ? The importance of encapsulation and data hiding
- ? How to use access modifiers to control visibility
- ? The
this
keyword for object reference - ? Key differences between Java OOP and C procedural programming