Model real things with classes and objects, protect data, reuse code with inheritance, and handle errors.
Every code example has a ▶ Run button. Click ✎ Edit, or press L, to open the Code Lab.
java Main.javajava Main.java in the folder D:\java-practice.Product, Cart, Order as classesnew. One class, many objects, each with its own values.| Real life | Java |
|---|---|
| House plan | class |
| Each house built | object |
| Rooms, colour | fields |
| Open door, switch on light | methods |
Student, BankAccount.1class Student {2 String name; // fields (data)3 int roll;4 double marks;56 void showReport() { // method: behaviour7 String result = marks >= 33 ? "PASS" : "FAIL";8 System.out.println(roll + ". " + name + " - " + marks + " - " + result);9 }10}1112public class Main {13 public static void main(String[] args) {14 Student s1 = new Student(); // create an object15 s1.name = "Aditi";16 s1.roll = 12;17 s1.marks = 91.5;1819 Student s2 = new Student();20 s2.name = "Rohan";21 s2.roll = 7;22 s2.marks = 28;2324 s1.showReport(); // call a method on an object25 s2.showReport();26 }27}
s1.name, s1.showReport(). Inside the method, marks means this object's marks.public class, named like the file.new Student() builds an object on the heap. The variable s1 holds only a reference (arrow) to it. Student s3 = s1; copies the arrow, not the object.new allocates memory on the heap1class Book {2 String title;3 String author;4 double price;56 Book(String title, String author, double price) {7 this.title = title; // field = parameter8 this.author = author;9 this.price = price;10 }1112 Book(String title) { // overloaded13 this(title, "Unknown", 199); // calls the first14 }1516 void show() {17 System.out.printf("%-28s %-18s Rs %.0f%n", title, author, price);18 }19}2021public class Main {22 public static void main(String[] args) {23 Book b1 = new Book("Godan", "Munshi Premchand", 250);24 Book b2 = new Book("Wings of Fire", "A P J Abdul Kalam", 399);25 Book b3 = new Book("Class Notes");26 b1.show();27 b2.show();28 b3.show();29 }30}
new. this means "the object being built"; this(...) calls another constructor.Each object has its own fields. Watch a and b hold different data.
1class Account {2 String holder;3 double balance;45 Account(String holder, double opening) {6 this.holder = holder;7 this.balance = opening;8 }910 void deposit(double amt) {11 balance += amt;12 }13}1415public class Main {16 public static void main(String[] args) {17 Account a = new Account("Ravi", 500);18 a.deposit(1500);19 Account b = new Account("Meena", 2000);20 System.out.println(a.holder + ": " + a.balance);21 System.out.println(b.holder + ": " + b.balance);22 }23}
1class Product {2 String name;3 double price;4 int qty;56 Product(String name, double price, int qty) {7 this.name = name;8 this.price = price;9 this.qty = qty;10 }1112 double total() {13 return price * qty;14 }1516 @Override17 public String toString() {18 return name + " x" + qty + " = Rs " + total();19 }20}2122public class Main {23 public static void main(String[] args) {24 Product p = new Product("Pen", 10, 5);25 System.out.println(p); // calls toString() automatically26 System.out.println("Item: " + p); // also here27 }28}
toString() method. Without it you get something like Product@1b6d3586. Override it to give a readable description.@Override tells the compiler you mean to replace an inherited method (Module 11).1class Item {2 String name;3 double price;4 int qty;56 Item(String n, double p, int q) { name = n; price = p; qty = q; }78 double amount() { return price * qty; }9}1011public class Main {12 public static void main(String[] args) {13 Item[] cart = {14 new Item("Basmati rice 5kg", 545, 1),15 new Item("Mustard oil 1L", 165, 2),16 new Item("Thekua pack", 120, 3)17 };18 double total = 0;19 for (Item it : cart) {20 System.out.printf("%-18s %2d x %6.2f = %8.2f%n", it.name, it.qty, it.price, it.amount());21 total += it.amount();22 }23 System.out.printf("%-18s %24.2f%n", "TOTAL", total);24 }25}
Item[] cart. Each element is a reference to an Item object. A for-each loop processes them one by one.ArrayList<Item> so the cart can grow (Module 15).1class Student {2 String name;3 Student(String name) { this.name = name; }4}56public class Main {7 public static void main(String[] args) {8 Student[] seats = new Student[3]; // 3 empty seats: all null9 seats[0] = new Student("Aman");10 for (int i = 0; i < seats.length; i++) {11 if (seats[i] != null) {12 System.out.println("Seat " + i + ": " + seats[i].name);13 } else {14 System.out.println("Seat " + i + ": empty");15 }16 }17 Student x = null;18 System.out.println(x.name); // crash!19 }20}
null means "points to no object". Using a dot on null (x.name) throws NullPointerException. Check != null before using a reference that may be empty.java Main.java. No JDK yet? Open an online Java compiler and paste the code (Copy).totalData() = days x dataPerDayboolean ott and print it in toString1class Rectangle {2 double length, breadth;34 Rectangle(double l, double b) { length = l; breadth = b; }56 double area() { return length * breadth; }7 double perimeter() { return 2 * (length + breadth); }8}910public class Rect {11 public static void main(String[] args) {12 Rectangle[] floors = {new Rectangle(12, 10), new Rectangle(15, 14), new Rectangle(8, 6)};13 for (Rectangle r : floors) {14 System.out.printf("%.0f x %.0f -> area %.0f sq ft, perimeter %.0f ft, tiles (2x2) %d%n",15 r.length, r.breadth, r.area(), r.perimeter(), (int) Math.ceil(r.area() / 4));16 }17 }18}
1class Student {2 String name;3 int std;4 double marks;56 Student(String name, int std, double marks) {7 this.name = name;8 this.std = std;9 this.marks = marks;10 }1112 public String toString() {13 return name + " (Class " + std + ") - " + marks + "%";14 }15}1617public class Register {18 public static void main(String[] args) {19 Student a = new Student("Priya Tirkey", 8, 88.5);20 Student b = new Student("Rahul Mahto", 8, 76.0);21 Student c = new Student("Zoya Khan", 9, 93.0);22 System.out.println(a);23 System.out.println(b);24 System.out.println(c);25 }26}
this(...) to avoid repeating code.1class Book {2 String title, author;3 double price;45 Book(String title, String author, double price) {6 this.title = title;7 this.author = author;8 this.price = price;9 }1011 Book(String title, String author) {12 this(title, author, 0);13 }1415 void show() {16 System.out.println(title + " by " + author + " - " + (price == 0 ? "Donated" : "Rs " + price));17 }18}1920public class Library {21 public static void main(String[] args) {22 new Book("Godan", "Premchand", 250).show();23 new Book("Jungle Book", "Rudyard Kipling").show();24 new Book("Ignited Minds", "A P J Abdul Kalam", 299).show();25 }26}
1class Account {2 String holder;3 String accNo;4 double balance;56 Account(String holder, String accNo, double opening) {7 this.holder = holder;8 this.accNo = accNo;9 balance = opening;10 }1112 void deposit(double amt) {13 balance += amt;14 System.out.println("Deposited " + amt + ", balance " + balance);15 }1617 void withdraw(double amt) {18 if (amt > balance) {19 System.out.println("Withdraw " + amt + " FAILED: insufficient balance");20 } else {21 balance -= amt;22 System.out.println("Withdrew " + amt + ", balance " + balance);23 }24 }25}2627public class Bank {28 public static void main(String[] args) {29 Account acc = new Account("Sunita Devi", "JH0091", 1000);30 acc.deposit(2500);31 acc.withdraw(5000);32 acc.withdraw(1200);33 System.out.println(acc.holder + " (" + acc.accNo + ") final balance: Rs " + acc.balance);34 }35}
1class Car {2 String model;3 int km;4 Car(String model, int km) { this.model = model; this.km = km; }5}67public class SameObject {8 public static void main(String[] args) {9 Car c1 = new Car("Alto", 12000);10 Car c2 = c1; // same car, two names11 c2.km += 350; // trip to Netarhat12 System.out.println("c1.km = " + c1.km);13 System.out.println("c1 == c2 ? " + (c1 == c2));14 Car c3 = new Car("Alto", 12350);15 System.out.println("c1 == c3 ? " + (c1 == c3) + " (different objects, same data)");16 }17}
1class Product {2 String name;3 double price, gstRate;4 int qty;56 Product(String name, double price, int qty, double gstRate) {7 this.name = name; this.price = price; this.qty = qty; this.gstRate = gstRate;8 }9 double subtotal() { return price * qty; }10 double gst() { return subtotal() * gstRate / 100; }11}1213public class Cart {14 public static void main(String[] args) {15 Product[] items = {16 new Product("Atta 10kg", 420, 1, 0),17 new Product("Biscuits", 30, 4, 18),18 new Product("Ghee 1L", 610, 1, 12),19 new Product("Soap", 45, 3, 18)20 };21 double sub = 0, tax = 0;22 System.out.printf("%-10s %4s %8s %7s%n", "Item", "Qty", "Amount", "GST");23 for (Product p : items) {24 System.out.printf("%-10s %4d %8.2f %7.2f%n", p.name, p.qty, p.subtotal(), p.gst());25 sub += p.subtotal();26 tax += p.gst();27 }28 System.out.printf("Subtotal %.2f + GST %.2f = Rs %.2f%n", sub, tax, sub + tax);29 }30}
Time add(int minutes) returns a new Time1class Time {2 int h, m;3 Time(int h, int m) { this.h = h; this.m = m; }45 Time add(int minutes) {6 int total = h * 60 + m + minutes;7 return new Time((total / 60) % 24, total % 60);8 }910 public String toString() { return String.format("%02d:%02d", h, m); }11}1213public class BusTime {14 public static void main(String[] args) {15 String[] to = {"Jamshedpur", "Dhanbad", "Patna"};16 Time[] dep = {new Time(6, 30), new Time(14, 15), new Time(21, 45)};17 int[] mins = {165, 210, 540};18 for (int i = 0; i < to.length; i++) {19 System.out.println("Ranchi " + dep[i] + " -> " + to[i] + " " + dep[i].add(mins[i]));20 }21 }22}
1class Ticket {2 int pnr, seat;3 String name;4 Ticket(int pnr, String name, int seat) { this.pnr = pnr; this.name = name; this.seat = seat; }5 public String toString() { return "PNR " + pnr + " | " + name + " | seat " + seat; }6}78class Train {9 static int nextPnr = 4510001;10 String name;11 Ticket[] booked;12 int count = 0;1314 Train(String name, int seats) { this.name = name; booked = new Ticket[seats]; }1516 Ticket book(String passenger) {17 if (count == booked.length) {18 System.out.println("Sorry " + passenger + ", " + name + " is full (waiting list)");19 return null;20 }21 Ticket t = new Ticket(nextPnr++, passenger, count + 1);22 booked[count++] = t;23 return t;24 }25}2627public class Irctc {28 public static void main(String[] args) {29 Train t = new Train("18626 Hatia-Purnea Exp", 3);30 String[] people = {"Anil", "Babita", "Chandan", "Deepa"};31 for (String p : people) t.book(p);32 System.out.println("Chart for " + t.name + ":");33 for (int i = 0; i < t.count; i++) System.out.println(" " + t.booked[i]);34 }35}
1class Book {2 String title;3 boolean issued;4 Book(String t) { title = t; }5}67class Member {8 String name;9 int holding;10 Member(String n) { name = n; }11}1213class Library {14 static final int LIMIT = 2;1516 static void issue(Member m, Book b) {17 if (b.issued) System.out.println("X " + b.title + " is already issued");18 else if (m.holding == LIMIT) System.out.println("X " + m.name + " already has " + LIMIT + " books");19 else {20 b.issued = true;21 m.holding++;22 System.out.println("OK " + b.title + " -> " + m.name);23 }24 }2526 static void giveBack(Member m, Book b) {27 b.issued = false;28 m.holding--;29 System.out.println("RETURNED " + b.title + " by " + m.name);30 }31}3233public class SchoolLibrary {34 public static void main(String[] args) {35 Book b1 = new Book("Panchatantra"), b2 = new Book("Wings of Fire"), b3 = new Book("Malgudi Days");36 Member asha = new Member("Asha"), vikas = new Member("Vikas");37 Library.issue(asha, b1);38 Library.issue(vikas, b1);39 Library.issue(asha, b2);40 Library.issue(asha, b3);41 Library.giveBack(asha, b1);42 Library.issue(vikas, b1);43 System.out.println(asha.name + " holds " + asha.holding + ", " + vikas.name + " holds " + vikas.holding);44 }45}
1class Student {2 String name;3 int[] marks;4 Student(String name, int... marks) { this.name = name; this.marks = marks; }5 int total() { int t = 0; for (int m : marks) t += m; return t; }6 double percent() { return total() * 100.0 / (marks.length * 100); }7 String grade() { double p = percent(); return p >= 90 ? "A+" : p >= 75 ? "A" : p >= 60 ? "B" : "C"; }8}910public class Merit {11 public static void main(String[] args) {12 Student[] s = {13 new Student("Aman", 78, 85, 90), new Student("Bina", 92, 95, 89),14 new Student("Chirag", 55, 61, 70), new Student("Divya", 88, 79, 93)15 };16 for (int i = 0; i < s.length - 1; i++) {17 int best = i;18 for (int j = i + 1; j < s.length; j++) if (s[j].percent() > s[best].percent()) best = j;19 Student t = s[i]; s[i] = s[best]; s[best] = t; // swap references20 }21 for (int i = 0; i < s.length; i++) {22 System.out.printf("%d. %-7s %3d %5.1f%% %s%n", i + 1, s[i].name, s[i].total(), s[i].percent(), s[i].grade());23 }24 }25}
newobj.field, obj.method()this, this(...)toString() for readable printingnull = no object; dot on null -> NullPointerExceptionWhat creates an object?
new ClassName(...) creates an object.A constructor has...
Printing an object without toString shows...
balance = -5000: fields are privatestatic counters generate order numbers, invoice numbers, roll numbersmodel, service, util1class Account {2 String holder;3 double balance; // anyone can change this directly!4}56public class Main {7 public static void main(String[] args) {8 Account a = new Account();9 a.holder = "Ravi";10 a.balance = 5000;11 a.balance = -99999; // nonsense, but Java allows it12 System.out.println(a.holder + " balance: " + a.balance);13 }14}
private and give public methods to read (getter) or change (setter) them. The methods can check every change.| Part | Example |
|---|---|
| private field | private double balance; |
| getter | public double getBalance() |
| setter | public void setAge(int a) |
| action method | deposit(), withdraw() |
1class Account {2 private String holder;3 private double balance;45 Account(String holder) { this.holder = holder; }67 public double getBalance() { return balance; } // getter8 public String getHolder() { return holder; }910 public void deposit(double amt) {11 if (amt <= 0) {12 System.out.println("Invalid amount: " + amt);13 return;14 }15 balance += amt;16 }1718 public boolean withdraw(double amt) {19 if (amt > balance) return false;20 balance -= amt;21 return true;22 }23}2425public class Main {26 public static void main(String[] args) {27 Account a = new Account("Ravi");28 a.deposit(5000);29 a.deposit(-99999); // rejected30 System.out.println("Withdraw 8000? " + a.withdraw(8000));31 System.out.println(a.getHolder() + " balance: " + a.getBalance());32 // a.balance = 1; // compile ERROR: private33 }34}
balance. The only ways in are deposit and withdraw, and both validate. There is no setBalance on purpose.getX(); for boolean fields isX(). Setter names: setX(value). IDEs generate them for you.1class Student {2 private String name;3 private int age;4 private String email;56 public void setName(String name) {7 if (name == null || name.isBlank()) throw new IllegalArgumentException("Name required");8 this.name = name.trim();9 }10 public void setAge(int age) {11 if (age < 3 || age > 25) {12 System.out.println("Rejected age " + age);13 return;14 }15 this.age = age;16 }17 public void setEmail(String email) {18 if (email.contains("@")) this.email = email.toLowerCase();19 else System.out.println("Rejected email " + email);20 }21 public String toString() { return name + ", " + age + ", " + email; }22}2324public class Main {25 public static void main(String[] args) {26 Student s = new Student();27 s.setName(" Pooja Kumari ");28 s.setAge(150);29 s.setAge(14);30 s.setEmail("pooja.gmail.com");31 s.setEmail("Pooja@Gmail.com");32 System.out.println(s);33 }34}
| Modifier | Same class | Same package | Subclass | Everywhere |
|---|---|---|---|---|
private | ✅ | ❌ | ❌ | ❌ |
| (default, none) | ✅ | ✅ | ❌* | ❌ |
protected | ✅ | ✅ | ✅ | ❌ |
public | ✅ | ✅ | ✅ | ✅ |
* a subclass in the same package can see default members
private, methods the outside needs public, helper methods private.static field has one copy for the whole class, shared by every object. A static method belongs to the class, so you call it with the class name: Math.sqrt(16), Student.getCount().| instance | static | |
|---|---|---|
| copies | one per object | one per class |
| call with | obj.method() | Class.method() |
| can use | all fields | only static fields |
| example | name, marks | count, GST_RATE |
All three objects share one count. Each object keeps its own roll.
1class Student {2 static int count = 0;3 String name;4 int roll;56 Student(String name) {7 count++;8 this.name = name;9 this.roll = count;10 }11}1213public class Main {14 public static void main(String[] args) {15 Student a = new Student("Aman");16 Student b = new Student("Bina");17 Student c = new Student("Chirag");18 System.out.println(c.name + " got roll " + c.roll);19 System.out.println("Total students: " + Student.count);20 }21}
1final class GstUtil { // final class: cannot be extended2 public static final double STANDARD = 18; // constant: UPPER_CASE3 public static final double FOOD = 5;45 private GstUtil() { } // nobody can create objects67 public static double withGst(double amount, double rate) {8 return Math.round(amount * (1 + rate / 100) * 100) / 100.0;9 }10}1112public class Main {13 public static void main(String[] args) {14 System.out.println("Phone : " + GstUtil.withGst(14999, GstUtil.STANDARD));15 System.out.println("Thali : " + GstUtil.withGst(180, GstUtil.FOOD));16 // GstUtil.FOOD = 0; // ERROR: final cannot change17 }18}
final variable = constant (value set once). static final = one shared constant, named in UPPER_CASE. A class with only static methods and a private constructor is a utility class, like Math and Arrays.1my-school-app/2 Main.java3 com/brainbit/school/Student.java4 com/brainbit/billing/Invoice.java
1package com.brainbit.school;23public class Student {4 private String name;5 private int marks;67 public Student(String name, int marks) {8 this.name = name;9 this.marks = marks;10 }1112 public String getName() { return name; }13 public int getMarks() { return marks; }14}
package com.brainbit.school; must match the folder path. Other code uses it with import.1import com.brainbit.school.Student;23public class Main {4 public static void main(String[] args) {5 Student s = new Student("Kiran", 88);6 System.out.println(s.getName() + " scored " + s.getMarks());7 // s.marks = 100; // ERROR: marks is private8 }9}
com.brainbit. Java's own: java.util, java.time.java Main.java. No JDK yet? Open an online Java compiler and paste the code (Copy).discount field with a setter that only allows 0-50%getFinalPrice()p.stock = 100; and read the compiler error1class Student {2 private String name;3 private int std;45 public String getName() { return name; }6 public void setName(String name) { this.name = name; }7 public int getStd() { return std; }8 public void setStd(int std) { this.std = std; }9}1011public class Portal {12 public static void main(String[] args) {13 Student s = new Student();14 s.setName("Neha Lakra");15 s.setStd(7);16 System.out.println(s.getName() + " studies in class " + s.getStd());17 s.setStd(8);18 System.out.println("Promoted to class " + s.getStd());19 }20}
1class Temperature {2 private double celsius;34 public void setCelsius(double c) {5 if (c < -50 || c > 60) {6 System.out.println("Rejected reading: " + c);7 return;8 }9 celsius = c;10 }11 public double getCelsius() { return celsius; }12 public double getFahrenheit() { return celsius * 9 / 5 + 32; }13}1415public class Weather {16 public static void main(String[] args) {17 Temperature t = new Temperature();18 double[] readings = {31.5, 999, 42.0, -80};19 for (double r : readings) {20 t.setCelsius(r);21 System.out.printf("Stored %.1f C = %.1f F%n", t.getCelsius(), t.getFahrenheit());22 }23 }24}
1class Ticket {2 private static int total = 0;3 private final int number;45 Ticket() {6 total++;7 number = 1000 + total;8 }9 public int getNumber() { return number; }10 public static int getTotal() { return total; }11}1213public class Visitors {14 public static void main(String[] args) {15 for (int i = 0; i < 4; i++) {16 Ticket t = new Ticket();17 System.out.println("Issued ticket #" + t.getNumber());18 }19 System.out.println("Visitors today: " + Ticket.getTotal());20 }21}
1final class MarksUtil {2 public static final int PASS_MARK = 33;3 private MarksUtil() { }45 public static double percent(int got, int total) { return got * 100.0 / total; }6 public static double average(int... m) {7 int s = 0;8 for (int x : m) s += x;9 return (double) s / m.length;10 }11 public static boolean isPass(int mark) { return mark >= PASS_MARK; }12}1314public class UseUtil {15 public static void main(String[] args) {16 System.out.printf("Percent: %.1f%n", MarksUtil.percent(412, 500));17 System.out.printf("Average: %.2f%n", MarksUtil.average(78, 91, 66, 84));18 System.out.println("30 pass? " + MarksUtil.isPass(30) + ", 45 pass? " + MarksUtil.isPass(45));19 }20}
private final, no setters; "changing" returns a new object.1final class Money {2 private final long paise;34 Money(double rupees) { paise = Math.round(rupees * 100); }5 private Money(long paise, boolean raw) { this.paise = paise; }67 Money add(Money other) { return new Money(paise + other.paise, true); }8 Money applyDiscount(int pct) { return new Money(paise * (100 - pct) / 100, true); }910 public String toString() { return String.format("Rs %d.%02d", paise / 100, paise % 100); }11}1213public class Payment {14 public static void main(String[] args) {15 Money shoes = new Money(1499.50);16 Money socks = new Money(199);17 Money total = shoes.add(socks);18 Money sale = total.applyDiscount(10);19 System.out.println("Shoes " + shoes + " + socks " + socks + " = " + total);20 System.out.println("After 10% off: " + sale);21 System.out.println("Original total still: " + total);22 }23}
1class Wallet {2 private static final double DAILY_LIMIT = 10000;3 private final String owner;4 private final int pin;5 private double balance, spentToday;67 Wallet(String owner, int pin) { this.owner = owner; this.pin = pin; }89 public void addMoney(double amt) { if (amt > 0) balance += amt; }1011 private boolean checkPin(int p) { return p == pin; }1213 public String pay(double amt, int p, String to) {14 if (!checkPin(p)) return "Wrong PIN";15 if (amt > balance) return "Insufficient balance";16 if (spentToday + amt > DAILY_LIMIT) return "Daily limit reached";17 balance -= amt;18 spentToday += amt;19 return "Paid Rs " + amt + " to " + to;20 }21 public double getBalance() { return balance; }22}2324public class WalletApp {25 public static void main(String[] args) {26 Wallet w = new Wallet("Sonu", 4321);27 w.addMoney(12000);28 System.out.println(w.pay(450, 1111, "Chai Point"));29 System.out.println(w.pay(450, 4321, "Chai Point"));30 System.out.println(w.pay(9000, 4321, "Electronics shop"));31 System.out.println(w.pay(800, 4321, "Kirana store"));32 System.out.println("Balance: Rs " + w.getBalance());33 }34}
1class Invoice {2 private static int next = 1;3 private static final String PREFIX = "INV/2026/";4 private final String number;5 private final String customer;6 private final double amount;78 private static String nextNumber() { return PREFIX + String.format("%04d", next++); }910 Invoice(String customer, double amount) {11 this.number = nextNumber();12 this.customer = customer;13 this.amount = amount;14 }15 public String toString() { return number + " " + customer + " Rs " + amount; }16}1718public class Billing {19 public static void main(String[] args) {20 System.out.println(new Invoice("Sharma Traders, Doranda", 15400));21 System.out.println(new Invoice("Mahto Hardware, Hinoo", 8250));22 System.out.println(new Invoice("Lal Medicals, Kanke", 3120));23 }24}
com/school/model/Student.javacom/school/util/Grade.javaOther files: com/school/model/Student.java (private name, marks + getters) and com/school/util/Grade.java (static of(int)). Open Edit in Lab to download Main; create the two folders yourself.
1import com.school.model.Student;2import com.school.util.Grade;34public class SchoolApp {5 public static void main(String[] args) {6 Student[] list = {new Student("Anjali", 91), new Student("Bittu", 64), new Student("Faiz", 78)};7 for (Student s : list) {8 System.out.println(s.getName() + " -> " + Grade.of(s.getMarks()));9 }10 }11}
1class Member {2 private static final int MAX_BOOKS = 3, PER_DAY = 2, CAP = 100;3 private final String name;4 private int borrowed;5 private int fineDue;67 Member(String name) { this.name = name; }89 public boolean borrow() {10 if (fineDue > 0 || borrowed == MAX_BOOKS) return false;11 borrowed++;12 return true;13 }14 public void returnBook(int daysLate) {15 if (borrowed == 0) return;16 borrowed--;17 fineDue += Math.min(daysLate * PER_DAY, CAP);18 }19 public void payFine(int amt) { fineDue = Math.max(0, fineDue - amt); }20 public String status() { return name + ": books " + borrowed + ", fine Rs " + fineDue; }21}2223public class LibraryRules {24 public static void main(String[] args) {25 Member m = new Member("Ritika");26 System.out.println("borrow: " + m.borrow() + " " + m.borrow());27 m.returnBook(12);28 System.out.println(m.status());29 System.out.println("borrow with fine? " + m.borrow());30 m.payFine(24);31 m.returnBook(90);32 System.out.println(m.status());33 m.payFine(100);34 System.out.println("borrow after paying? " + m.borrow() + " -> " + m.status());35 }36}
1class Employee {2 private static int seq = 0;3 private static final double MIN_BASIC = 10000;4 private final String id;5 private final String name;6 private double basic;78 Employee(String name, double basic) {9 this.id = String.format("EMP%03d", ++seq);10 this.name = name;11 setBasic(basic);12 }13 public void setBasic(double b) {14 if (b < MIN_BASIC) {15 System.out.println(" (basic " + b + " below minimum, set to " + MIN_BASIC + ")");16 b = MIN_BASIC;17 }18 basic = b;19 }20 public double getHra() { return basic * 0.20; }21 public double getDa() { return basic * 0.12; }22 public double getPf() { return basic * 0.12; }23 public double getNet() { return basic + getHra() + getDa() - getPf(); }24 public String slip() {25 return String.format("%s %-8s basic %8.0f hra %6.0f da %6.0f pf %6.0f net %8.0f", id, name, basic, getHra(), getDa(), getPf(), getNet());26 }27}2829public class Payroll {30 public static void main(String[] args) {31 Employee[] staff = {new Employee("Ajay", 32000), new Employee("Kavita", 8000), new Employee("Imran", 45500)};32 for (Employee e : staff) System.out.println(e.slip());33 }34}
private fields + public getters/settersstatic: one copy per class; call with the class namefinal = cannot change; static final constants in UPPER_CASEimport to use themWhich keeps a field visible only inside its class?
A static field is...
Where must package com.app; go?
Vehicle but charges each correctlyObjectActivity; exceptions extend Exceptionclass Car extends Vehicle means a Car is a Vehicle. Car inherits all of Vehicle's fields and methods, can add its own, and can override (replace) methods.| Word | Meaning |
|---|---|
| parent / superclass | Vehicle |
| child / subclass | Car, Bus, Truck |
| inherits | gets number, start() |
| overrides | own toll() |
1class Vehicle {2 String number;3 Vehicle(String number) { this.number = number; }4 void start() { System.out.println(number + " engine started"); }5 int toll() { return 50; }6}78class Car extends Vehicle {9 int seats;10 Car(String number, int seats) {11 super(number); // run the parent constructor first12 this.seats = seats;13 }14}1516class Bus extends Vehicle {17 Bus(String number) { super(number); }18 @Override19 int toll() { return 290; } // Bus changes the rule20}2122public class Main {23 public static void main(String[] args) {24 Car c = new Car("JH01AB1234", 5);25 Bus b = new Bus("JH01PA0042");26 c.start(); // inherited, not written in Car27 b.start();28 System.out.println("Car toll: " + c.toll() + ", seats " + c.seats);29 System.out.println("Bus toll: " + b.toll());30 }31}
super(...) calls the parent constructor and must be the first line of the child constructor. Car gets start() and toll() for free; Bus overrides toll().@Override asks the compiler to check you really are overriding. A spelling mistake becomes an error instead of a silent bug.1class Employee {2 String name;3 double basic;4 Employee(String name, double basic) { this.name = name; this.basic = basic; }5 double salary() { return basic + basic * 0.20; } // basic + HRA6 public String toString() { return name + " Rs " + salary(); }7}89class Manager extends Employee {10 double bonus;11 Manager(String name, double basic, double bonus) {12 super(name, basic);13 this.bonus = bonus;14 }15 @Override16 double salary() { return super.salary() + bonus; } // reuse parent logic17}1819class Intern extends Employee {20 Intern(String name, double stipend) { super(name, stipend); }21 @Override22 double salary() { return basic; } // no HRA23}2425public class Main {26 public static void main(String[] args) {27 System.out.println(new Employee("Ravi", 30000));28 System.out.println(new Manager("Sunita", 50000, 15000));29 System.out.println(new Intern("Kunal", 12000));30 }31}
super.salary() calls the parent version, so the child adds only the difference.| Overloading | Overriding |
|---|---|
| same class | parent and child |
| different parameters | same parameters |
| chosen at compile time | chosen at run time |
Polymorphism ("many forms"): the same call p.fee(2000) runs a different method depending on the actual object. Watch which line lights up.
1class Payment {2 double fee(double amt) { return 0; }3}4class Upi extends Payment {5 double fee(double amt) { return 0; }6}7class Card extends Payment {8 double fee(double amt) { return amt * 0.02; }9}10class Wallet extends Payment {11 double fee(double amt) { return Math.min(amt * 0.01, 15); }12}1314public class Main {15 public static void main(String[] args) {16 Payment[] ways = {new Upi(), new Card(), new Wallet()};17 double total = 0;18 for (Payment p : ways) {19 double f = p.fee(2000);20 total += f;21 }22 System.out.println("Total fees: " + total);23 }24}
1double pay(Payment p, double amt) {2 double f = p.fee(amt);3 System.out.println("Fee: " + f);4 return amt + f;5}67// Next year: class NetBanking extends Payment { ... }8// pay() works with it immediately!
Payment). New child classes plug in without editing the old code. This is the Open/Closed principle used in every large project.Payment p = new Card(); is automatic and safe.1import java.util.Objects;23class Student {4 String roll;5 String name;6 Student(String roll, String name) { this.roll = roll; this.name = name; }78 @Override9 public boolean equals(Object o) {10 if (!(o instanceof Student s)) return false;11 return roll.equals(s.roll); // same roll = same student12 }13 @Override14 public int hashCode() { return Objects.hash(roll); }15 @Override16 public String toString() { return roll + ":" + name; }17}1819public class Main {20 public static void main(String[] args) {21 Student a = new Student("R12", "Aditi");22 Student b = new Student("R12", "Aditi Kumari");23 System.out.println(a == b); // different objects24 System.out.println(a.equals(b)); // same student by our rule25 System.out.println(a);26 }27}
extends Object, which gives toString(), equals() and hashCode(). Override equals to define "same" for your objects, and always override hashCode with it (needed by HashMap/HashSet).1class Vehicle { String number; Vehicle(String n) { number = n; } }2class Car extends Vehicle { Car(String n) { super(n); } }3class Truck extends Vehicle {4 double loadTons;5 Truck(String n, double t) { super(n); loadTons = t; }6}78public class Main {9 public static void main(String[] args) {10 Vehicle[] queue = {new Car("JH01A1"), new Truck("JH05T9", 12.5), new Car("BR01C3")};11 for (Vehicle v : queue) {12 if (v instanceof Truck t) { // check AND cast13 int toll = t.loadTons > 10 ? 520 : 470;14 System.out.println(v.number + " truck, " + t.loadTons + " t -> Rs " + toll);15 } else {16 System.out.println(v.number + " car -> Rs 85");17 }18 }19 Vehicle x = new Car("JH09Z");20 Truck bad = (Truck) x; // ClassCastException!21 }22}
v instanceof Truck t checks the real type and, if true, gives a Truck variable t (pattern matching, Java 16+). A wrong manual cast throws ClassCastException.1class Engine { int cc; }2class Car extends Vehicle { // is a3 Engine engine; // has a4 Wheel[] wheels;5}
final class cannot be extended (String is final). final method cannot be overridden.java Main.java. No JDK yet? Open an online Java compiler and paste the code (Copy).Triangle (base, height) with its own area()perimeter() to Shape and override itSquare extends Rect using super(side, side)1class Animal {2 String name;3 Animal(String name) { this.name = name; }4 String sound() { return "..."; }5}6class Dog extends Animal { Dog() { super("Dog"); } String sound() { return "Bhow bhow"; } }7class Cow extends Animal { Cow() { super("Cow"); } String sound() { return "Moo"; } }8class Cat extends Animal { Cat() { super("Cat"); } String sound() { return "Meow"; } }910public class Sounds {11 public static void main(String[] args) {12 Animal[] zoo = {new Dog(), new Cow(), new Cat()};13 for (Animal a : zoo) System.out.println(a.name + " says " + a.sound());14 }15}
1class Person {2 String name;3 int age;4 Person(String name, int age) { this.name = name; this.age = age; }5 String info() { return name + ", " + age; }6}7class Student extends Person {8 int roll;9 Student(String n, int a, int roll) { super(n, a); this.roll = roll; }10 String info() { return super.info() + ", roll " + roll; }11}12class Teacher extends Person {13 String subject;14 Teacher(String n, int a, String s) { super(n, a); subject = s; }15 String info() { return super.info() + ", teaches " + subject; }16}1718public class School {19 public static void main(String[] args) {20 System.out.println(new Student("Ankit Oraon", 14, 23).info());21 System.out.println(new Teacher("Mrs. Ekka", 41, "Maths").info());22 }23}
1class Building {2 Building() { System.out.println("1. Building: foundation laid"); }3}4class House extends Building {5 House() { System.out.println("2. House: walls built"); }6}7class SmartHouse extends House {8 SmartHouse() { System.out.println("3. SmartHouse: Wi-Fi and sensors added"); }9}1011public class ChainOrder {12 public static void main(String[] args) {13 new SmartHouse();14 }15}
1class Vehicle {2 String no;3 Vehicle(String no) { this.no = no; }4 int toll() { return 0; }5}6class Car extends Vehicle { Car(String n) { super(n); } int toll() { return 85; } }7class Bus extends Vehicle { Bus(String n) { super(n); } int toll() { return 290; } }8class Truck extends Vehicle {9 double load;10 Truck(String n, double load) { super(n); this.load = load; }11 int toll() { return 470 + (load > 10 ? 50 : 0); }12}1314public class Toll {15 public static void main(String[] args) {16 Vehicle[] q = {new Car("JH01AB1"), new Truck("JH02T77", 14), new Bus("JH01PB5"), new Truck("WB19T3", 8), new Car("BR06C2")};17 int total = 0;18 for (Vehicle v : q) {19 System.out.printf("%-8s %-6s Rs %d%n", v.no, v.getClass().getSimpleName(), v.toll());20 total += v.toll();21 }22 System.out.println("Collected: Rs " + total);23 }24}
1class Account {2 protected double balance;3 void deposit(double a) { balance += a; }4 boolean withdraw(double a) {5 if (a > balance) return false;6 balance -= a;7 return true;8 }9 void addInterest() { }10}11class Savings extends Account {12 @Override void addInterest() { balance += balance * 0.04; }13}14class Current extends Account {15 @Override boolean withdraw(double a) {16 if (a > balance + 10000) return false;17 balance -= a;18 return true;19 }20}2122public class AccountTypes {23 public static void main(String[] args) {24 Account[] accs = {new Savings(), new Current()};25 for (Account a : accs) {26 a.deposit(5000);27 boolean ok = a.withdraw(12000);28 a.addInterest();29 System.out.println(a.getClass().getSimpleName() + ": withdraw 12000 " + (ok ? "OK" : "refused") + ", balance " + a.balance);30 }31 }32}
1class Employee {2 String name;3 Employee(String n) { name = n; }4 double salary() { return 0; }5}6class Permanent extends Employee {7 double basic;8 Permanent(String n, double b) { super(n); basic = b; }9 double salary() { return basic * 1.20; }10}11class Manager extends Permanent {12 double bonus;13 Manager(String n, double b, double bonus) { super(n, b); this.bonus = bonus; }14 double salary() { return super.salary() + bonus; }15}16class Contract extends Employee {17 int hours; double rate;18 Contract(String n, int h, double r) { super(n); hours = h; rate = r; }19 double salary() { return hours * rate; }20}2122public class SalarySheet {23 public static void main(String[] args) {24 Employee[] staff = {new Permanent("Asha", 30000), new Manager("Vikram", 55000, 12000), new Contract("Rakesh", 120, 250)};25 double total = 0;26 for (Employee e : staff) {27 System.out.printf("%-7s %-9s Rs %,10.2f%n", e.name, e.getClass().getSimpleName(), e.salary());28 total += e.salary();29 }30 System.out.printf("Total payroll: Rs %,.2f%n", total);31 }32}
1import java.util.Objects;23class Product {4 String code, name;5 Product(String c, String n) { code = c; name = n; }6 @Override public boolean equals(Object o) {7 return o instanceof Product p && code.equalsIgnoreCase(p.code);8 }9 @Override public int hashCode() { return Objects.hash(code.toUpperCase()); }10}1112public class UniqueStock {13 public static void main(String[] args) {14 Product[] scanned = {new Product("P100", "Rice"), new Product("p100", "rice 5kg"), new Product("P200", "Dal"), new Product("P300", "Oil"), new Product("P200", "Toor dal")};15 int unique = 0;16 for (int i = 0; i < scanned.length; i++) {17 boolean seen = false;18 for (int j = 0; j < i; j++) if (scanned[i].equals(scanned[j])) seen = true;19 if (!seen) { unique++; System.out.println("New: " + scanned[i].code + " " + scanned[i].name); }20 }21 System.out.println("Unique products: " + unique + " of " + scanned.length + " scans");22 }23}
1class Notifier {2 void send(String to, String msg) {3 System.out.println("[" + getClass().getSimpleName() + " -> " + to + "] " + format(msg));4 }5 String format(String msg) { return msg; }6 double cost() { return 0; }7}8class Sms extends Notifier {9 String format(String m) { return m.length() > 40 ? m.substring(0, 40) + "..." : m; }10 double cost() { return 0.15; }11}12class Email extends Notifier {13 String format(String m) { return "Hello,\n " + m + "\n Regards, BrainBit Infotech"; }14}15class WhatsApp extends Notifier {16 String format(String m) { return "*OTP* " + m; }17 double cost() { return 0.35; }18}1920public class SendOtp {21 public static void main(String[] args) {22 String otp = "Your OTP is 482913. Valid for 10 minutes. Do not share it.";23 Notifier[] ch = {new Sms(), new Email(), new WhatsApp()};24 String[] to = {"98xxxxxx10", "riya@gmail.com", "98xxxxxx10"};25 double cost = 0;26 for (int i = 0; i < ch.length; i++) {27 ch[i].send(to[i], otp);28 cost += ch[i].cost();29 }30 System.out.println("Cost: Rs " + cost);31 }32}
1class Hero {2 protected String name;3 protected int hp = 100;4 Hero(String n) { name = n; }5 void act(Hero[] team, int[] boss) { }6}7class Warrior extends Hero {8 Warrior() { super("Warrior"); }9 void act(Hero[] t, int[] boss) { boss[0] -= 18; }10}11class Archer extends Hero {12 Archer() { super("Archer"); }13 void act(Hero[] t, int[] boss) { boss[0] -= 12; boss[0] -= 12; }14}15class Healer extends Hero {16 Healer() { super("Healer"); }17 void act(Hero[] t, int[] boss) {18 Hero weak = t[0];19 for (Hero h : t) if (h.hp < weak.hp) weak = h;20 weak.hp = Math.min(100, weak.hp + 15);21 }22}2324public class Battle {25 public static void main(String[] args) {26 Hero[] team = {new Warrior(), new Archer(), new Healer()};27 int[] boss = {150};28 int[] bossHits = {25, 30, 20};29 for (int round = 1; round <= 3 && boss[0] > 0; round++) {30 team[round % 3].hp -= bossHits[round - 1]; // boss attacks31 for (Hero h : team) h.act(team, boss);32 System.out.printf("Round %d: boss %d | ", round, Math.max(boss[0], 0));33 for (Hero h : team) {34 String tag = (h instanceof Healer) ? "+" : "";35 System.out.print(tag + h.name + " " + h.hp + " ");36 }37 System.out.println();38 }39 System.out.println(boss[0] <= 0 ? "Boss defeated!" : "Boss survives");40 }41}
1class Shape { double area() { return 0; } }2class Rect extends Shape { double l, b; Rect(double l, double b) { this.l = l; this.b = b; } double area() { return l * b; } }3class Circle extends Shape { double r; Circle(double r) { this.r = r; } double area() { return Math.PI * r * r / 2; } } // half circle bay4class Triangle extends Shape { double b, h; Triangle(double b, double h) { this.b = b; this.h = h; } double area() { return 0.5 * b * h; } }56class Room {7 String name;8 Shape[] parts; // HAS-A9 Room(String name, Shape... parts) { this.name = name; this.parts = parts; }10 double area() {11 double a = 0;12 for (Shape s : parts) a += s.area();13 return a;14 }15}1617public class FloorQuote {18 public static void main(String[] args) {19 Room[] rooms = {new Room("Hall", new Rect(16, 12), new Circle(4)), new Room("Bedroom", new Rect(12, 11), new Triangle(4, 3))};20 double total = 0;21 for (Room r : rooms) {22 System.out.printf("%-8s %7.2f sq ft Rs %,10.2f%n", r.name, r.area(), r.area() * 85);23 total += r.area() * 85;24 }25 System.out.printf("Quote total: Rs %,.2f%n", total);26 }27}
class Child extends Parent: inherit fields and methods ("is a")super(...) first in the constructor; super.method() reuses parent logic@OverrideObject: toString, equals, hashCodeinstanceof pattern matching; prefer composition for "has a"Payment p = new Card(); p.fee(100); runs...
How many classes can a Java class extend?
Where must super(...) appear?
List is an interface: ArrayList and LinkedList implement itConnection; MySQL, Oracle and SQLite drivers implement itPaymentGateway; switching provider changes one class1abstract class Shape {2 String name;3 Shape(String name) { this.name = name; }45 abstract double area(); // no body: children MUST write it67 void describe() { // normal method, shared by all8 System.out.printf("%-9s area = %.2f%n", name, area());9 }10}1112class Circle extends Shape {13 double r;14 Circle(double r) { super("Circle"); this.r = r; }15 double area() { return Math.PI * r * r; }16}1718class Square extends Shape {19 double s;20 Square(double s) { super("Square"); this.s = s; }21 double area() { return s * s; }22}2324public class Main {25 public static void main(String[] args) {26 Shape[] list = {new Circle(2), new Square(3)};27 for (Shape sh : list) sh.describe();28 // Shape x = new Shape("?"); // ERROR: Shape is abstract29 }30}
abstract class is an incomplete parent: you cannot create objects of it. An abstract method has no body; every concrete child must override it. Normal methods and fields are still shared.implements it promises to provide all of them. Unrelated classes (Invoice, Salary, Bill) can share one interface.| Interface | Real meaning |
|---|---|
Payable | can be paid |
Comparable | can be sorted |
Runnable | can run in a thread |
AutoCloseable | can be closed |
1interface Payable {2 double amount(); // public abstract automatically3 String payTo();4}56class Invoice implements Payable {7 String vendor; double total;8 Invoice(String v, double t) { vendor = v; total = t; }9 public double amount() { return total; }10 public String payTo() { return vendor; }11}1213class Salary implements Payable {14 String emp; double basic;15 Salary(String e, double b) { emp = e; basic = b; }16 public double amount() { return basic * 1.2; }17 public String payTo() { return emp; }18}1920public class Main {21 public static void main(String[] args) {22 Payable[] dues = {new Invoice("Sharma Stationery", 12500), new Salary("Asha", 30000), new Invoice("JBVNL", 4300)};23 double sum = 0;24 for (Payable p : dues) {25 System.out.printf("Pay %-18s Rs %,10.2f%n", p.payTo(), p.amount());26 sum += p.amount();27 }28 System.out.printf("Total outgoing: Rs %,.2f%n", sum);29 }30}
class X implements Payable must define every interface method as public. Then a Payable variable can hold an Invoice, a Salary, or anything that signs the contract.1interface Printable {2 void print();3}4interface Exportable {5 String toCsv();6 default String fileName() { return "report.csv"; } // ready-made method7}89class MarkSheet implements Printable, Exportable {10 String name; int[] marks;11 MarkSheet(String n, int... m) { name = n; marks = m; }1213 public void print() {14 System.out.println("Mark sheet of " + name + ": " + java.util.Arrays.toString(marks));15 }16 public String toCsv() {17 StringBuilder sb = new StringBuilder(name);18 for (int m : marks) sb.append(",").append(m);19 return sb.toString();20 }21}2223public class Main {24 public static void main(String[] args) {25 MarkSheet ms = new MarkSheet("Riya", 88, 92, 79);26 ms.print();27 System.out.println(ms.fileName() + " -> " + ms.toCsv());28 Printable p = ms; // seen as just Printable29 p.print();30 }31}
extend only one class but implement many interfaces. A default method gives a ready body that implementers may keep or override. Interfaces can also have static methods and constants.| abstract class | interface | |
|---|---|---|
| keyword | extends | implements |
| how many | one | many |
| fields | any fields | only constants |
| constructors | yes | no |
| methods | abstract + normal | abstract + default + static |
| relation | "is a" family | "can do" ability |
| example | Shape, Employee | Payable, Comparable |
class Bus extends Vehicle implements Trackable.checkout() never changes; we pass different Discount objects. Adding a new discount type needs no change to checkout.
1interface Discount {2 double apply(double price);3}4class Festive implements Discount {5 public double apply(double p) { return p * 0.80; }6}7class Member implements Discount {8 public double apply(double p) { return p - 100; }9}1011public class Main {12 static double checkout(double price, Discount d) {13 double fin = d.apply(price);14 return fin;15 }16 public static void main(String[] args) {17 double a = checkout(1000, new Festive());18 double b = checkout(1000, new Member());19 System.out.println(a + " " + b);20 }21}
1@FunctionalInterface2interface Rule {3 boolean test(int marks);4}56public class Main {7 static void show(int[] marks, Rule r, String label) {8 System.out.print(label + ": ");9 for (int m : marks) if (r.test(m)) System.out.print(m + " ");10 System.out.println();11 }1213 public static void main(String[] args) {14 int[] marks = {91, 28, 67, 45, 99, 33};15 show(marks, new Rule() { // anonymous class (old way)16 public boolean test(int m) { return m >= 33; }17 }, "Pass");18 show(marks, m -> m >= 90, "Distinction"); // lambda (Module 17)19 show(marks, m -> m < 33, "Fail");20 }21}
m -> m >= 90.Runnable, Comparator, Predicate, Function.java Main.java. No JDK yet? Open an online Java compiler and paste the code (Copy).TableExporter that prints aligned columns with paddingHtmlExporter that outputs <tr><td> rowsExporter.of(String type) that returns the right exporter1abstract class Shape {2 abstract double area();3 void show() { System.out.printf("%s: %.2f%n", getClass().getSimpleName(), area()); }4}5class Rectangle extends Shape {6 double l, b;7 Rectangle(double l, double b) { this.l = l; this.b = b; }8 double area() { return l * b; }9}10class Triangle extends Shape {11 double b, h;12 Triangle(double b, double h) { this.b = b; this.h = h; }13 double area() { return 0.5 * b * h; }14}1516public class Shapes {17 public static void main(String[] args) {18 new Rectangle(12, 8).show();19 new Triangle(10, 6).show();20 }21}
1interface Switchable {2 void on();3 void off();4}5class Fan implements Switchable {6 public void on() { System.out.println("Fan spinning at speed 3"); }7 public void off() { System.out.println("Fan stopped"); }8}9class Light implements Switchable {10 public void on() { System.out.println("LED light ON (9W)"); }11 public void off() { System.out.println("LED light OFF"); }12}1314public class SmartHome {15 public static void main(String[] args) {16 Switchable[] room = {new Fan(), new Light()};17 for (Switchable s : room) s.on();18 for (Switchable s : room) s.off();19 }20}
1interface Gst {2 double FOOD = 5, MEDICINE = 12, STANDARD = 18, LUXURY = 28;3 static double withTax(double price, double rate) { return price + price * rate / 100; }4}56public class TaxDemo {7 public static void main(String[] args) {8 System.out.println("Biscuits : " + Gst.withTax(40, Gst.FOOD));9 System.out.println("Syrup : " + Gst.withTax(120, Gst.MEDICINE));10 System.out.println("Charger : " + Gst.withTax(599, Gst.STANDARD));11 System.out.println("AC : " + Gst.withTax(38000, Gst.LUXURY));12 }13}
1interface PaymentGateway {2 String pay(double amount);3 double feePercent();4}5class RazorLike implements PaymentGateway {6 private int seq = 100;7 public String pay(double a) { return "rzp_" + (seq++); }8 public double feePercent() { return 2.0; }9}10class PaytmLike implements PaymentGateway {11 private int seq = 5000;12 public String pay(double a) { return "PTM" + (seq++); }13 public double feePercent() { return 1.75; }14}1516public class Checkout {17 static void checkout(PaymentGateway g, double amount) {18 String id = g.pay(amount);19 double fee = amount * g.feePercent() / 100;20 System.out.printf("%s: Rs %.2f paid, fee Rs %.2f, txn %s%n", g.getClass().getSimpleName(), amount, fee, id);21 }22 public static void main(String[] args) {23 checkout(new RazorLike(), 1499);24 checkout(new PaytmLike(), 1499);25 }26}
1abstract class Employee {2 String name;3 Employee(String n) { name = n; }4 abstract double pay();5 void payslip() {6 System.out.println("---- PAYSLIP ----");7 System.out.printf("%-10s Rs %,.2f%n", name, pay());8 }9}10class FullTime extends Employee {11 double monthly;12 FullTime(String n, double m) { super(n); monthly = m; }13 double pay() { return monthly; }14}15class PartTime extends Employee {16 int hours; double rate;17 PartTime(String n, int h, double r) { super(n); hours = h; rate = r; }18 double pay() { return hours * rate; }19}2021public class Payslips {22 public static void main(String[] args) {23 Employee[] all = {new FullTime("Anita", 42000), new PartTime("Manoj", 64, 300), new FullTime("Sabir", 28500)};24 for (Employee e : all) e.payslip();25 }26}
Comparable<T>) lets Arrays.sort work with your class.1import java.util.Arrays;23class Student implements Comparable<Student> {4 String name; int marks;5 Student(String n, int m) { name = n; marks = m; }6 public int compareTo(Student o) { return Integer.compare(o.marks, marks); } // descending7 public String toString() { return name + "(" + marks + ")"; }8}910public class Ranks {11 public static void main(String[] args) {12 Student[] s = {new Student("Aman", 78), new Student("Bina", 95), new Student("Chhotu", 61), new Student("Diya", 88)};13 Arrays.sort(s);14 for (int i = 0; i < s.length; i++) System.out.println((i + 1) + ". " + s[i]);15 }16}
1interface Exporter {2 String export(String[][] rows);3 static Exporter of(String type) {4 return switch (type) {5 case "csv" -> new Csv();6 case "json" -> new Json();7 default -> new Table();8 };9 }10}11class Csv implements Exporter {12 public String export(String[][] r) {13 StringBuilder sb = new StringBuilder();14 for (String[] row : r) sb.append(String.join(",", row)).append("\n");15 return sb.toString();16 }17}18class Table implements Exporter {19 public String export(String[][] r) {20 StringBuilder sb = new StringBuilder();21 for (String[] row : r) sb.append(String.format("| %-8s | %5s |%n", row[0], row[1]));22 return sb.toString();23 }24}25class Json implements Exporter {26 public String export(String[][] r) {27 StringBuilder sb = new StringBuilder("[");28 for (int i = 1; i < r.length; i++) {29 sb.append(String.format("{\"%s\":\"%s\",\"%s\":%s}", r[0][0], r[i][0], r[0][1], r[i][1]));30 if (i < r.length - 1) sb.append(",");31 }32 return sb.append("]\n").toString();33 }34}3536public class ExportAll {37 public static void main(String[] args) {38 String[][] data = {{"name", "marks"}, {"Aman", "88"}, {"Bina", "93"}};39 for (String t : new String[]{"csv", "table", "json"}) System.out.print(Exporter.of(t).export(data));40 }41}
1abstract class Vehicle {2 String no; double fuel;3 Vehicle(String no, double fuel) { this.no = no; this.fuel = fuel; }4 abstract double mileage();5 double range() { return fuel * mileage(); }6}7interface Trackable {8 String location();9}10class CityBus extends Vehicle implements Trackable {11 String stop;12 CityBus(String no, double f, String stop) { super(no, f); this.stop = stop; }13 double mileage() { return 4.5; }14 public String location() { return "near " + stop; }15}16class Auto extends Vehicle implements Trackable {17 Auto(String no, double f) { super(no, f); }18 double mileage() { return 25; }19 public String location() { return "Main Road"; }20}21class Truck extends Vehicle {22 Truck(String no, double f) { super(no, f); }23 double mileage() { return 3.2; }24}2526public class Fleet {27 public static void main(String[] args) {28 Vehicle[] all = {new CityBus("JH01PB1101", 60, "Firayalal Chowk"), new Auto("JH01AU4521", 6), new Truck("JH01TC7788", 120)};29 for (Vehicle v : all) {30 String loc = (v instanceof Trackable t) ? t.location() : "not tracked";31 System.out.printf("%-11s range %6.1f km %s%n", v.no, v.range(), loc);32 }33 }34}
1abstract class Question {2 String text; int marks;3 Question(String t, int m) { text = t; marks = m; }4 abstract boolean check(String ans);5 double penalty() { return 0; }6}7class Mcq extends Question {8 char key;9 Mcq(String t, char k) { super(t, 2); key = k; }10 boolean check(String a) { return a.equalsIgnoreCase(String.valueOf(key)); }11 double penalty() { return 0.25 * marks; }12}13class TrueFalse extends Question {14 boolean key;15 TrueFalse(String t, boolean k) { super(t, 1); key = k; }16 boolean check(String a) { return Boolean.parseBoolean(a) == key; }17}18class Numeric extends Question {19 double key, tol;20 Numeric(String t, double k, double tol) { super(t, 3); key = k; this.tol = tol; }21 boolean check(String a) { return Math.abs(Double.parseDouble(a) - key) <= tol; }22}2324public class OnlineTest {25 public static void main(String[] args) {26 Question[] paper = {27 new Mcq("Capital of Jharkhand? a) Dhanbad b) Ranchi", 'b'),28 new TrueFalse("Hundru falls is near Ranchi", true),29 new Numeric("Area of circle r=2 (2 decimals)", 12.57, 0.01),30 new Mcq("Java keyword to inherit? a) implements b) extends", 'b')31 };32 String[] answers = {"B", "true", "12.566", "a"};33 double score = 0; int max = 0;34 for (int i = 0; i < paper.length; i++) {35 Question q = paper[i];36 boolean ok = q.check(answers[i]);37 double got = ok ? q.marks : -q.penalty();38 score += got; max += q.marks;39 System.out.printf("Q%d %-10s %-6s %+5.2f%n", i + 1, q.getClass().getSimpleName(), ok ? "right" : "wrong", got);40 }41 System.out.printf("Score: %.2f / %d%n", score, max);42 }43}
1@FunctionalInterface2interface DiscountRule {3 double apply(double bill);4}5class Festival implements DiscountRule {6 int pct;7 Festival(int pct) { this.pct = pct; }8 public double apply(double b) { return b * (100 - pct) / 100; }9}1011public class DiscountEngine {12 public static void main(String[] args) {13 DiscountRule[] rules = {14 new Festival(10),15 b -> b - 150, // member flat Rs 15016 b -> b >= 2000 ? b - 200 : b, // coupon above 200017 b -> Math.max(b, 0)18 };19 String[] names = {"Diwali 10%", "Member -150", "Coupon -200", "Floor at 0"};20 double bill = 2600;21 System.out.println("Bill: Rs " + bill);22 for (int i = 0; i < rules.length; i++) {23 bill = rules[i].apply(bill);24 System.out.printf(" after %-12s Rs %.2f%n", names[i], bill);25 }26 }27}
abstract class: cannot be instantiated; may have abstract and normal methodsimplements; methods are public abstract by defaultdefault and static methodsCan you write new Shape() if Shape is abstract?
How many interfaces can a class implement?
A functional interface has exactly...
1import java.util.Scanner;23public class Main {4 public static void main(String[] args) {5 Scanner sc = new Scanner(System.in);6 System.out.print("Total bill: ");7 int bill = sc.nextInt();8 System.out.print("Number of friends: ");9 int friends = sc.nextInt();10 System.out.println("Each pays: " + bill / friends);11 System.out.println("Thank you!"); // never reached12 }13}
1import java.util.Scanner;2import java.util.InputMismatchException;34public class Main {5 public static void main(String[] args) {6 Scanner sc = new Scanner(System.in);7 for (int k = 0; k < 3; k++) {8 try {9 System.out.print("Bill and friends: ");10 int bill = sc.nextInt();11 int friends = sc.nextInt();12 System.out.println(" Each pays: " + bill / friends);13 } catch (ArithmeticException e) {14 System.out.println(" Friends cannot be zero (" + e.getMessage() + ")");15 } catch (InputMismatchException e) {16 System.out.println(" Please type numbers only");17 sc.nextLine(); // throw away the bad input18 }19 }20 System.out.println("Program finished normally");21 }22}
try. If an exception happens, Java jumps to the matching catch block, then continues after it. Several catch blocks can handle different problems.Exception.Watch line 7 with "12x": the exception skips the rest of try and lands in catch. finally always runs.
1public class Main {2 public static void main(String[] args) {3 String[] inputs = {"250", "12x", "40"};4 int total = 0;5 for (String s : inputs) {6 try {7 int n = Integer.parseInt(s);8 total += n;9 } catch (NumberFormatException e) {10 System.out.println("Skipped bad value: " + s);11 } finally {12 System.out.println("checked " + s);13 }14 }15 System.out.println("Total = " + total);16 }17}
IOException) are problems outside your control; the compiler forces you to catch or declare them. Unchecked ones (RuntimeException family) are usually bugs or bad input. Errors are serious JVM problems you should not catch.| Exception | Typical cause |
|---|---|
| ArithmeticException | divide by zero |
| NullPointerException | dot on null |
| ArrayIndexOutOfBounds | bad index |
| NumberFormatException | parseInt("abc") |
| IOException | file / network problem |
1public class Main {2 static double withdraw(double balance, double amount) {3 if (amount <= 0) {4 throw new IllegalArgumentException("Amount must be positive");5 }6 if (amount > balance) {7 throw new IllegalStateException("Insufficient balance: " + balance);8 }9 return balance - amount;10 }1112 public static void main(String[] args) {13 double bal = 5000;14 double[] tries = {1500, -20, 9000, 2000};15 for (double t : tries) {16 try {17 bal = withdraw(bal, t);18 System.out.println("Withdrew " + t + ", balance " + bal);19 } catch (IllegalArgumentException | IllegalStateException e) {20 System.out.println("Refused " + t + ": " + e.getMessage());21 }22 }23 }24}
throw new XException("message") stops the method and sends the problem to the caller. The caller decides how to handle it. One catch can handle several types with |.1class InsufficientFundsException extends Exception { // checked2 private final double shortBy;3 InsufficientFundsException(double shortBy) {4 super("Short by Rs " + shortBy);5 this.shortBy = shortBy;6 }7 double getShortBy() { return shortBy; }8}910class Account {11 private double balance = 3000;12 void withdraw(double amt) throws InsufficientFundsException { // warns callers13 if (amt > balance) throw new InsufficientFundsException(amt - balance);14 balance -= amt;15 System.out.println("Dispensed Rs " + amt);16 }17}1819public class Main {20 public static void main(String[] args) {21 Account acc = new Account();22 try {23 acc.withdraw(2000);24 acc.withdraw(2500);25 } catch (InsufficientFundsException e) {26 System.out.println("ATM: " + e.getMessage() + ". Try a smaller amount.");27 }28 }29}
Exception (checked) or RuntimeException (unchecked). A method that may throw a checked exception must say so with throws.| throw | throws |
|---|---|
| inside a method body | in the method header |
| actually raises one | warns it may happen |
throw new X() | void f() throws X |
1class Printer implements AutoCloseable {2 Printer() { System.out.println("Printer connected"); }3 void print(String doc) {4 if (doc.isEmpty()) throw new IllegalArgumentException("Empty document");5 System.out.println("Printing: " + doc);6 }7 public void close() { System.out.println("Printer released"); }8}910public class Main {11 public static void main(String[] args) {12 try (Printer p = new Printer()) { // closed automatically13 p.print("Fee receipt #4521");14 p.print("");15 } catch (IllegalArgumentException e) {16 System.out.println("Error: " + e.getMessage());17 }18 System.out.println("Done");19 }20}
try (Resource r = ...) closes it automatically; the resource must implement AutoCloseable.Exceptionthrow with a clear message1// BAD: hides the problem2try { save(order); } catch (Exception e) { }34// GOOD: specific, informative, logged5try {6 save(order);7} catch (SQLException e) {8 log.error("Order " + order.id + " not saved", e);9 showMessage("Could not place order. Please retry.");10}
java Main.java. No JDK yet? Open an online Java compiler and paste the code (Copy).readMobile() that accepts only 10 digits starting with 6-9InvalidMobileException and throw itfinally that counts attempts1public class SafeSplit {2 static void split(int bill, int friends) {3 try {4 System.out.println("Rs " + bill + " / " + friends + " = Rs " + bill / friends + " each");5 } catch (ArithmeticException e) {6 System.out.println("Cannot split among 0 friends");7 }8 }910 public static void main(String[] args) {11 split(1200, 4);12 split(1200, 0);13 split(999, 3);14 }15}
1public class ParseAll {2 public static void main(String[] args) {3 String[] form = {"45", "seventy", "88", " 91 ", "9.5", ""};4 int good = 0, sum = 0;5 for (String f : form) {6 try {7 int v = Integer.parseInt(f.trim());8 sum += v;9 good++;10 } catch (NumberFormatException e) {11 System.out.println("Bad value: \"" + f + "\"");12 }13 }14 System.out.println(good + " valid numbers, sum " + sum);15 }16}
1public class QuizPick {2 public static void main(String[] args) {3 String[] q = {"Capital of Jharkhand?", "Largest waterfall near Ranchi?", "Java founder?", "2 + 2 * 2 = ?"};4 int[] picks = {2, 7, 0};5 for (int p : picks) {6 try {7 System.out.println("Q" + p + ": " + q[p]);8 } catch (ArrayIndexOutOfBoundsException e) {9 System.out.println("No question number " + p);10 } finally {11 System.out.println("---");12 }13 }14 }15}
1import java.util.Scanner;23public class TrainNo {4 public static void main(String[] args) {5 Scanner sc = new Scanner(System.in);6 while (true) {7 System.out.print("Train number: ");8 String s = sc.nextLine().trim();9 try {10 int n = Integer.parseInt(s);11 if (s.length() != 5) throw new IllegalArgumentException("must be 5 digits");12 System.out.println("Searching train " + n + "...");13 break;14 } catch (NumberFormatException e) {15 System.out.println(" digits only please");16 } catch (IllegalArgumentException e) {17 System.out.println(" " + e.getMessage());18 }19 }20 }21}
1public class VoterForm {2 static void validate(String name, int age) {3 if (name == null || name.isBlank()) throw new IllegalArgumentException("name is required");4 if (age < 18) throw new IllegalArgumentException(name + " is under 18");5 if (age > 120) throw new IllegalArgumentException("age " + age + " looks wrong");6 }78 public static void main(String[] args) {9 String[] names = {"Ramesh Munda", "", "Kiran", "Lakhan"};10 int[] ages = {34, 25, 16, 140};11 for (int i = 0; i < names.length; i++) {12 try {13 validate(names[i], ages[i]);14 System.out.println("Accepted: " + names[i]);15 } catch (IllegalArgumentException e) {16 System.out.println("Rejected: " + e.getMessage());17 }18 }19 }20}
throws in the method header.1class AtmException extends Exception {2 AtmException(String msg) { super(msg); }3}45class Atm {6 private double balance = 12000, today = 0;7 void withdraw(int amt) throws AtmException {8 if (amt % 100 != 0) throw new AtmException("Enter amount in multiples of 100");9 if (amt > balance) throw new AtmException("Insufficient balance");10 if (today + amt > 10000) throw new AtmException("Daily limit Rs 10,000 exceeded");11 balance -= amt;12 today += amt;13 System.out.println("Please collect Rs " + amt);14 }15 double getBalance() { return balance; }16}1718public class AtmDemo {19 public static void main(String[] args) {20 Atm atm = new Atm();21 int[] req = {2550, 4000, 5000, 2000, 500};22 for (int r : req) {23 try {24 atm.withdraw(r);25 } catch (AtmException e) {26 System.out.println("Sorry: " + e.getMessage());27 }28 }29 System.out.println("Balance: Rs " + atm.getBalance());30 }31}
e.getStackTrace() and the chain of calls.1public class TraceRead {2 static int parseLine(String line) {3 String[] p = line.split(",");4 return Integer.parseInt(p[1].trim());5 }6 static int loadMarks(String[] lines) {7 int sum = 0;8 for (String l : lines) sum += parseLine(l);9 return sum;10 }11 public static void main(String[] args) {12 String[] file = {"Aman, 78", "Bina, 9O", "Chirag, 66"}; // 9O has letter O!13 try {14 System.out.println(loadMarks(file));15 } catch (NumberFormatException e) {16 System.out.println("Problem: " + e.getMessage());17 for (StackTraceElement st : e.getStackTrace()) {18 if (st.getClassName().equals("TraceRead")) System.out.println(" at " + st.getMethodName() + " line " + st.getLineNumber());19 }20 }21 }22}
new OrderException(msg, cause).1class StockException extends RuntimeException {2 StockException(String m) { super(m); }3}4class OrderException extends Exception {5 OrderException(String m, Throwable cause) { super(m, cause); }6}78class Shop {9 int stock = 5;10 void reserve(int qty) {11 if (qty > stock) throw new StockException("only " + stock + " left");12 stock -= qty;13 }14 void placeOrder(int id, int qty) throws OrderException {15 try {16 reserve(qty);17 System.out.println("Order #" + id + " confirmed (" + qty + ")");18 } catch (StockException e) {19 throw new OrderException("Order #" + id + " failed", e);20 }21 }22}2324public class Orders {25 public static void main(String[] args) {26 Shop s = new Shop();27 int[][] orders = {{101, 2}, {102, 4}, {103, 3}};28 for (int[] o : orders) {29 try {30 s.placeOrder(o[0], o[1]);31 } catch (OrderException e) {32 System.out.println(e.getMessage() + " because " + e.getCause().getMessage());33 }34 }35 }36}
1class InvalidMarksException extends Exception {2 InvalidMarksException(int m) { super("marks " + m + " not in 0-100"); }3}45public class Importer {6 static int parse(String row) throws InvalidMarksException {7 String[] f = row.split(",");8 int m = Integer.parseInt(f[1].trim());9 if (m < 0 || m > 100) throw new InvalidMarksException(m);10 return m;11 }1213 public static void main(String[] args) {14 String[] rows = {"Aman,78", "Bina,abc", "Chirag", "Diya,105", "Eshan, 64", "Farah,91"};15 int ok = 0, bad = 0, sum = 0, line = 0;16 for (String r : rows) {17 try {18 sum += parse(r);19 ok++;20 } catch (NumberFormatException e) {21 bad++; System.out.println("Row " + (line + 1) + " skipped: not a number");22 } catch (ArrayIndexOutOfBoundsException e) {23 bad++; System.out.println("Row " + (line + 1) + " skipped: missing marks column");24 } catch (InvalidMarksException e) {25 bad++; System.out.println("Row " + (line + 1) + " skipped: " + e.getMessage());26 } finally {27 line++;28 }29 }30 System.out.printf("Imported %d, skipped %d, average %.1f%n", ok, bad, (double) sum / ok);31 }32}
1class Connection implements AutoCloseable {2 Connection() { System.out.println("DB connection opened"); }3 void save(String b) {4 if (b.contains("Sunday")) throw new IllegalStateException("Lab closed on Sunday: " + b);5 System.out.println("Saved " + b);6 }7 public void close() { System.out.println("DB connection closed"); }8}9class Logger implements AutoCloseable {10 Logger() { System.out.println("Log file opened"); }11 void log(String m) { System.out.println(" [log] " + m); }12 public void close() { System.out.println("Log file closed"); }13}1415public class LabBooking {16 public static void main(String[] args) {17 String[] bookings = {"Mon 10AM Physics", "Tue 2PM Chemistry", "Sunday 9AM Biology", "Wed 11AM Computer"};18 try (Connection c = new Connection(); Logger log = new Logger()) {19 for (String b : bookings) {20 c.save(b);21 log.log("booked " + b);22 }23 } catch (IllegalStateException e) {24 System.out.println("Stopped: " + e.getMessage());25 }26 System.out.println("System ready for next request");27 }28}
try risky code, catch specific types, finally always runsthrow new X("msg"); throws in the method headerextends Exception or RuntimeExceptionInteger.parseInt("12a") throws...
Which block runs whether or not an exception occurs?
IOException is a...
You finished Object-Oriented Java: 5 modules and 50 practice examples.
D:\java-practice\part3java -versionD:\java-practicejava Main.java