CS & IT
ENGINEERING
Java With OOPs
OOPs using Java
Lecture No-04 By- Aditya sir
Recap of Previous Lecture
Topic
Inheriter
Topic
Encapsulation
Topics to be Covered
Topic Java alcaur
Collections
Encapsulation
Inheritance
Misc concepts
3
Topic : super() – Invoking Superclass Constructors
• Role & Syntax
○ super(arg1,…) calls a matching constructor in the immediate parent class.
○ Must be the first statement in the subclass constructor.
• Implicit vs. Explicit
○ If omitted, Java injects super() only if the parent has a no-arg constructor.
○ Compilation error otherwise.
• Multilevel Flow
○ In a chain A←B←C:
▪ C() calls B() via super(), then B() calls A(), then A()’s body runs.
• Compile-Time Checks
○ Cannot call two different super ctors in one ctor.
○ Cannot place super() after any other statement.
Topic : super() – Invoking Superclass Constructors
• Use Case:
○ Subclassing GUI components (e.g., class MyFrame extends JFrame {
MyFrame(){ super("Title"); … } }).
Topic : [Link]() – Accessing Overridden Behavior
• Purpose
○ Extend or wrap parent logic rather than completely replace it.
• Syntax
• @Override
• void foo() {
• [Link](); // parent behavior
• // subclass additions
}
• Binding
○ Statically binds to the immediate superclass’s version of foo().
Topic : [Link]() – Accessing Overridden Behavior
• Order Matters
○ Call it before or after subclass code to control sequencing.
• Pitfall
○ Forgetting it can skip essential base-class work (e.g., cleanup, logging).
• Use Case
○ Augmenting framework callbacks (e.g., in servlets, filters).
Topic : Field Hiding & [Link]
• What Is Hiding?
○ Subclass declares a field with the same name as in its parent → hides the
parent’s field.
• Access Routes
○ [Link] → subclass’s own field
○ [Link] → hidden parent field
• Why It Happens
○ Sometimes needed to override default configuration values.
• Drawbacks
○ Can confuse maintainers; use sparingly.
• Use Case
○ Wrapping base-class settings without modifying the base itself.
Topic : Field Hiding & [Link]
• Example Snippet
class A { int x = 1; }
class B extends A {
int x = 2;
void show(){ [Link](super.x + "," + this.x); }
}
Topic : Multilevel Inheritance & Constructor Flow
• Definition: Class C extends B, B extends A.
• Execution Order:
○ new C() → C’s ctor → super() → B’s ctor → super() → A’s ctor → then B’s
body → C’s body.
• Implicit super()
○ If you don’t write any, compiler injects no-arg super().
• Required when
○ Parent has only parameterized ctors → must call explicitly.
• Use Case
○ Layered frameworks (e.g., request → security filter → business logic) where
each adds its own init.
• Diagram: Show arrow chain A←B←C.
Topic : Polymorphism & super Interaction
• Normal Dispatch: [Link]() → most specific override at runtime.
• Inside Override: [Link]() → immediate parent’s version, regardless of
deeper overrides.
• Code Binding: Determined at compile time for super calls.
• Example Flow:
• class A{ void f(){print("A");} }
class B extends A{ @Override void f(){print("B");} }
class C extends B{ @Override void f(){ super.f(); print("C"); } }
A x = new C(); x.f(); // prints B then C
• Use Case
○ Guarantee that an intermediate class’s logic runs before subclass’s.
Topic : Best Practices & Guidelines
Example 1 – Animal Sounds (Basic Polymorphism)
• Problem: Implement [Link](). Subclasses Dog and Cat override speak().
In main, store them in an array of Animal and call speak() on each.
• Hint: Use @Override in Dog/Cat, and a single main inside Example1.
Topic : Best Practices & Guidelines
• Code:
public class Example1 {
static class Animal {
void speak() { [Link]("Some sound"); }
}
static class Dog extends Animal {
@Override void speak() { [Link]("Woof"); }
}
static class Cat extends Animal {
@Override void speak() { [Link]("Meow"); }
}
public static void main(String[] args) {
Animal[] zoo = { new Animal(), new Dog(), new Cat() };
for (Animal a : zoo) [Link]();
}
}
Topic : Best Practices & Guidelines
• Expected Output:
Some sound
Woof
Meow
Topic : Best Practices & Guidelines
Example 2 – Shape Areas (Abstract Base)
• Problem: Abstract class Shape with double area(). Implement Circle and
Rectangle, then print their areas formatted to two decimals.
• Hint: Override area(). Use a single main in Example2.
Topic : Best Practices & Guidelines
• Code:
public class Example2 {
static abstract class Shape {
abstract double area(); not
}
static class Circle extends Shape {
double r;
Circle(double r){ this.r = r; }
@Override double area(){ return [Link] * r * r; }
}
static class Rectangle extends Shape {
2F
double w, h;
Rectangle(double w, double h){ this.w = w; this.h = h; }
@Override double area(){ return w * h; }
}
public static void main(String[] args) {
Shape[] shapes = { new Circle(2), new Rectangle(3, 4) };
for (Shape s : shapes)
}
[Link]("%.2f%n", [Link]());
}
decimal
u
Topic : Best Practices & Guidelines
• Expected Output:
12.57
12.00
Topic : Best Practices & Guidelines
Example 3 – Employee Bonus ([Link])
• Problem : [Link]() returns base salary.
Manager extends Employee, overrides getSalary() to add a 10% bonus but still
use the base calculation.
• Hint: In [Link](), call [Link]().
Topic : Best Practices & Guidelines
• Code:
public class Example3 {
static class Employee {
double base;
Employee(double base){ [Link] = base; }
double getSalary(){ return base; }
}
static class Manager extends Employee {
Manager(double base){ super(base); }
@Override double getSalary(){
return [Link]() * 1.10; // 10% bonus
}
}
public static void main(String[] args) {
Employee e = new Employee(1000), m = new Manager(1000);
[Link]([Link]());
[Link]([Link]());
}
}
Topic : Best Practices & Guidelines
• Expected Output:
1000.0
1100.0
THANK - YOU