1. Before Java: What are we trying to learn?
Java is a programming language. But in school-level learning, the first target is not to memorize syntax. The first target is to understand how to convert a real-life situation into a program.
Real problem
A school wants to store student name, roll number, marks and attendance percentage.
Program thinking
Create a Student class. Each student object will store the data of one real student.
So Java is not only about typing commands. Java is about building clear models of real things.
2. What is OOP?
OOP means Object-Oriented Programming. It is a style of programming where we divide the program into objects.
A school itself can be understood through objects:
| Real school item | Java object idea | Useful data | Useful behaviour |
|---|---|---|---|
| Student | Student object |
name, roll number, marks | calculate percentage, display report |
| Fee receipt | FeeReceipt object |
receipt number, amount, date | print receipt, check due amount |
| Library book | LibraryBook object |
title, accession number, issued status | issue book, return book |
| Bus route | BusRoute object |
route number, driver, stops | show route, count stops |
3. OOP Terms with Realistic Examples
Student is the design of a student
record.riya can be one
student object.rollNo,
name, marks.calculatePercentage().Student s1.First realistic class: Student record
class Student {
int rollNo;
String name;
int maths;
int computer;
int totalMarks() {
return maths + computer;
}
double percentage() {
return totalMarks() / 2.0;
}
void printReport() {
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
System.out.println("Total: " + totalMarks());
System.out.println("Percentage: " + percentage());
}
}
class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.rollNo = 12;
s1.name = "Riya Sharma";
s1.maths = 88;
s1.computer = 96;
s1.printReport();
}
}
Here, Student is the class. s1 is the object reference. rollNo,
name, maths and computer are fields. totalMarks(),
percentage() and printReport() are methods.
4. Four Pillars of OOP with School Examples
4.1 Encapsulation
Encapsulation means keeping related data and methods together inside one class.
class FeeAccount {
private int totalFee;
private int paidFee;
FeeAccount(int totalFee, int paidFee) {
this.totalFee = totalFee;
this.paidFee = paidFee;
}
int dueAmount() {
return totalFee - paidFee;
}
void printStatus() {
System.out.println("Total Fee: " + totalFee);
System.out.println("Paid Fee: " + paidFee);
System.out.println("Due Amount: " + dueAmount());
}
}
The fee data and fee calculations are packed together. That is encapsulation.
4.2 Abstraction
Abstraction means showing only what is needed and hiding unnecessary details.
When the school office prints a fee due report, the operator does not need to know every internal formula. The operator only calls:
account.printStatus();
The internal calculation is hidden inside the class.
4.3 Inheritance
Inheritance means creating a new class from an existing class. Use it when the new thing is a special type of the old thing.
class Person {
String name;
String phone;
void printContact() {
System.out.println(name + " - " + phone);
}
}
class Student extends Person {
int rollNo;
void printStudentCard() {
System.out.println("Roll No: " + rollNo);
printContact();
}
}
class Teacher extends Person {
String subject;
void printTeacherCard() {
System.out.println("Subject: " + subject);
printContact();
}
}
A student and a teacher are both persons, so common information like name and phone can stay in the
Person class.
4.4 Polymorphism
Polymorphism means one name can have different forms.
In a school marks system, the word average may be used for two subjects, three subjects, or
five subjects.
class MarksCalculator {
double average(int a, int b) {
return (a + b) / 2.0;
}
double average(int a, int b, int c) {
return (a + b + c) / 3.0;
}
double average(int a, int b, int c, int d, int e) {
return (a + b + c + d + e) / 5.0;
}
}
This is called method overloading. The method name is the same, but the number of inputs is different.
5. Other Important Parts of Java Language
5.1 Basic program structure
class Main {
public static void main(String[] args) {
System.out.println("Welcome to Java");
}
}
| Part | Meaning |
|---|---|
class |
Used to create a class. |
Main |
Name of the class. |
main() |
Starting point of the program. |
System.out.println() |
Prints output on screen. |
5.2 Variables and data types
int rollNo = 15;
double percentage = 89.5;
char section = 'A';
boolean passed = true;
String studentName = "Arjun Verma";
int stores whole numbers, double stores decimal numbers, char stores
one character, boolean stores true or false, and String stores text.
5.3 Input using Scanner
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter roll number: ");
int rollNo = sc.nextInt();
System.out.print("Enter marks: ");
int marks = sc.nextInt();
System.out.println("Roll No " + rollNo + " scored " + marks);
}
}
5.4 Conditions
int attendance = 78;
if (attendance >= 75) {
System.out.println("Allowed to appear in exam");
} else {
System.out.println("Attendance shortage");
}
5.5 Loops
for (int rollNo = 1; rollNo <= 5; rollNo++) {
System.out.println("Printing admit card for roll no " + rollNo);
}
5.6 Arrays
int[] marks = {82, 91, 76, 88, 95};
int total = 0;
for (int i = 0; i < marks.length; i++) {
total = total + marks[i];
}
System.out.println("Total marks = " + total);
6. Realistic Complete Examples
Example 1: Library book issue system
class LibraryBook {
String title;
String accessionNo;
boolean issued;
LibraryBook(String title, String accessionNo) {
this.title = title;
this.accessionNo = accessionNo;
this.issued = false;
}
void issueBook() {
if (issued) {
System.out.println(title + " is already issued.");
} else {
issued = true;
System.out.println(title + " has been issued.");
}
}
void returnBook() {
issued = false;
System.out.println(title + " has been returned.");
}
}
class Main {
public static void main(String[] args) {
LibraryBook book = new LibraryBook("Introduction to Java", "LIB-1021");
book.issueBook();
book.issueBook();
book.returnBook();
}
}
Example 2: Attendance checker
class AttendanceRecord {
String studentName;
int totalDays;
int presentDays;
AttendanceRecord(String studentName, int totalDays, int presentDays) {
this.studentName = studentName;
this.totalDays = totalDays;
this.presentDays = presentDays;
}
double attendancePercentage() {
return presentDays * 100.0 / totalDays;
}
void printEligibility() {
System.out.println("Student: " + studentName);
System.out.println("Attendance: " + attendancePercentage() + "%");
if (attendancePercentage() >= 75) {
System.out.println("Exam status: Allowed");
} else {
System.out.println("Exam status: Shortage");
}
}
}
class Main {
public static void main(String[] args) {
AttendanceRecord a = new AttendanceRecord("Meera", 200, 160);
a.printEligibility();
}
}
Example 3: School transport route
class BusRoute {
int routeNo;
String driverName;
String[] stops;
BusRoute(int routeNo, String driverName, String[] stops) {
this.routeNo = routeNo;
this.driverName = driverName;
this.stops = stops;
}
void printRoute() {
System.out.println("Route No: " + routeNo);
System.out.println("Driver: " + driverName);
System.out.println("Stops:");
for (int i = 0; i < stops.length; i++) {
System.out.println((i + 1) + ". " + stops[i]);
}
}
}
class Main {
public static void main(String[] args) {
String[] stops = {"Gate No. 1", "City Library", "Railway Crossing", "Main Market"};
BusRoute route = new BusRoute(7, "Mr. Singh", stops);
route.printRoute();
}
}
7. Common Mistakes Students Make
= instead of == in conditions.
= assigns a value. == compares values.
int marks = 90;
if (marks == 90) {
System.out.println("Excellent");
}
==. Use
equals().
String house = "Blue";
if (house.equals("Blue")) {
System.out.println("Blue house student");
}
String[] subjects = {"English", "Maths", "Computer"};
System.out.println(subjects[0]); // English
8. Practice Projects: From 0 to Infinity
Level 0
Print student name, class and school name.
Level 1
Take marks input and print pass/fail.
Level 2
Use a loop to print admit cards for roll numbers 1 to 40.
Level 3
Use an array to store marks of five subjects and calculate total.
Level 4
Create a Student class and print report card.
Level 5
Create FeeAccount, LibraryBook and BusRoute classes.
Level 6
Use encapsulation with private fields and public getters/setters.
Infinity
Build a mini school management system using multiple classes.
9. Revision and Quick Quiz
One-page revision
Class: blueprint. Object: real record made from class. Field: data inside class. Method: action inside class. Constructor: object setup method. Encapsulation: data and methods together. Abstraction: show useful part, hide internal work. Inheritance: reuse common features. Polymorphism: one method name, multiple forms.
Quiz 1
In a report card program, what should Student usually be?
Quiz 2
Which one is a realistic method for a FeeAccount class?
Quiz 3
Which keyword creates an object in Java?