Day 29 of 30 · Final Stretch
Complete Java + OOPS revision
Revision day1 Learn
Revision day. Go through each block and say the idea in your own words.
Java basics: a class holds code; main is the entry point; variables have types (int, double, char, boolean, String); operators calculate and compare; if/else and switch decide; for/while/do-while repeat; methods take parameters and return values; arrays store lists from index 0; Strings are immutable objects compared with equals().
OOPS: a class is a blueprint and an object is an instance made with new; constructors set up objects; this is the current object; encapsulation = private fields + methods; inheritance = extends (is-a); super reaches the parent; polymorphism = overloading (compile time) + overriding (run time); abstraction = abstract classes and interfaces; access modifiers control visibility; packages organize classes.
2 See the Code
// One program, many concepts
abstract class Shape { // abstraction
private final String name; // encapsulation
Shape(String name) { this.name = name; } // constructor + this
String getName() { return name; }
abstract double area();
}
class Rect extends Shape { // inheritance
private final double w, h;
Rect(double w, double h) { super("Rect"); this.w = w; this.h = h; }
double area() { return w * h; } // overriding
}
class Sq extends Rect {
Sq(double s) { super(s, s); }
}
public class Revise {
public static void main(String[] args) {
Shape[] list = { new Rect(2, 3), new Sq(4) }; // array + polymorphism
for (Shape s : list) { // loop
String size = s.area() > 10 ? "big" : "small"; // decision
System.out.println(s.getName() + " " + s.area() + " " + size);
}
}
}- Match every comment to the concept you learned.
- Sq reuses Rect's constructor through super(s, s), and its name is still Rect.
Output
Rect 6.0 small Rect 16.0 big
3 Practice
- Write one sentence and one tiny example for each: class, object, constructor, encapsulation, inheritance, polymorphism, abstraction, interface.
- Revisit any Day where you got a Quick Check question wrong.
Try it in your own Java setup or write it in the Practice Notebook.
4 Quick Check
1. Integer division: 9 / 2 = ?
2. Which is decided at run time?
3. A class can extend how many classes directly?
4. Private fields + public methods is called...
Step 5
Answer and check the Quick Check above to unlock this.