0% found this document useful (0 votes)
10 views18 pages

OOP Concepts in Java: Classes & Inheritance

The document covers key concepts of Object-Oriented Programming (OOP) using Java, including classes, objects, constructors, encapsulation, inheritance, and practical mini-projects. It provides examples and hints for projects like a Calculator, Student, Bank, and Shape applications, emphasizing the use of OOP principles such as inheritance and method overriding. The lecture is presented by Aditya sir and includes a recap of previous topics and a focus on practical coding exercises.

Uploaded by

omglokesh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

OOP Concepts in Java: Classes & Inheritance

The document covers key concepts of Object-Oriented Programming (OOP) using Java, including classes, objects, constructors, encapsulation, inheritance, and practical mini-projects. It provides examples and hints for projects like a Calculator, Student, Bank, and Shape applications, emphasizing the use of OOP principles such as inheritance and method overriding. The lecture is presented by Aditya sir and includes a recap of previous topics and a focus on practical coding exercises.

Uploaded by

omglokesh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CS & IT

ENGINEERING
Java With OOPs
OOPs using Java

Lecture No-07 By- Aditya sir


Recap of Previous Lecture

Topic Constructors, this & Intro to Inheritance

Practice full
Topics to be Covered

Topic Classes & Objects OOPS Applications

Java DSA practice

3
Topic : Classes & Objects

• Class: class MyClass { … } defines fields and methods.


• Object: MyClass obj = new MyClass();—an instance with its own state.
• Fields vs. Methods: Fields store data; methods implement behavior.
• Instance vs. Static: Instance members per object; static shared across the class.
• Memory: References on the stack → objects on the heap.
• Design: Sketch UML class diagrams before coding.
Bonus
Topic : Constructors & this Keyword

• Default Constructor: Auto-generated if none provided.


• Parameterized Constructor: Initialize fields with arguments.
• Overloading: Multiple constructors with different signatures.
• Chaining: this(...) to call another constructor in the same class.
• [Link]: Disambiguates between parameter and field name.
• Rule: this() call must be the first statement in a constructor.

attr parameter

[Link] name
fieldman
Topic : Encapsulation & Access Modifiers

• Private Fields: Hidden from outside—only class can access.


• Getters/Setters: Control and validate access and mutation.
• Protected & Package: protected for subclass and package; default is package-private.
• Immutability: Omit setters to make objects read-only after creation.
women
• Validation: Enforce invariants
mimmo (e.g., non-negative balances).
• Benefits: Reduces coupling, preserves invariants.
Topic : Inheritance & super Keyword

• extends: Subclass inherits fields and methods of superclass.


• “is-a”: Cat extends Animal ⇒ every Cat is an Animal.
• Overriding: Subclass provides new behavior for inherited methods.
• super(): Invoke parent constructor.
• [Link](): Call parent’s implementation in override.
• Single Inheritance: One direct superclass per class.
Process

Problem
1 Question
Industry Take
Assumptions
27 problem
3 Own understanding of

47 Share Final Code as Text on Telegram


Talon 2 double
I int

I boolean
as Ip
user
from print
them
Topic : Practical Mini-Projects
1. CalculatorApp
Problem statement:
Create a Calculator with basic int/float operations, then extend it to a Scientific Calculator adding
trigonometric (sin, cos) and power functions.
Hint
• Use inheritance (extends Calculator).
• Overload add(int,int) and add(double,double).
• Instantiate and invoke both basic and scientific methods.
Topic : Practical Mini-Projects
2. StudentApp
Problem statement:
Model a Student with academic marks and extend to SportsStudent to include an athletic score in the
average calculation.
Hint
• Override calculateAverage() in SportsStudent.
• Combine [Link]() and the sports score.
Code:
public class StudentApp {
public static void main(String[] args) {
int[] marks = {80, 90, 75};
Student s = new Student("Alice", 101, marks);
[Link]("Academic Avg: " + [Link]());
SportsStudent ss = new SportsStudent("Bob", 102, marks, 85);
[Link]("Overall Avg: " + [Link]());
}
}
class Student {
private String name;
private int rollNo;
private int[] marks;
public Student(String name, int rollNo, int[] marks) {
[Link] = name; [Link] = rollNo; [Link] = marks;
}
public double calculateAverage() {
int sum = 0; for (int m : marks) sum += m;
return (double) sum / [Link];
}
}
class SportsStudent extends Student {
private int sportsScore;
public SportsStudent(String name, int rollNo, int[] marks, int sportsScore) {
super(name, rollNo, marks);
[Link] = sportsScore;
}
@Override
public double calculateAverage() {
return ([Link]() + sportsScore) / 2.0;
}
}
Expected Output:
Academic Avg: 81.66666666666667
Overall Avg: 83.33333333333333
Topic : Practical Mini-Projects
3. BankApp
Problem Statement:
Design an abstract BankAccount that supports deposit and withdraw, and implement interest
calculation in SavingsAccount and CheckingAccount.
Hint
• Use an abstract class and call super(accountNo, balance) in subclass constructors.
• Each subclass provides its own calculateInterest().
Topic : Practical Mini-Projects
5. ShapeApp
Problem Statement: Imp
Create an abstract Shape with area() and perimeter(), then implement Circle and Rectangle and
display their metrics.
Hint
• Store different Shape instances in an array and loop over them.
Code:
public class ShapeApp {
public static void main(String[] args) {
Shape[] shapes = { new Circle(2.5), new Rectangle(4, 3) };
for (Shape s : shapes) {
[Link]([Link]().getSimpleName()
+ " area=" + [Link]()
+ " perim=" + [Link]());
}
}
}
abstract class Shape {
public abstract double area();
public abstract double perimeter();
}
class Circle extends Shape {
private double r;
public Circle(double r) { this.r = r; }
public double area() { return [Link] * r * r; }
public double perimeter() { return 2 * [Link] * r; }
}
class Rectangle extends Shape {
private double w, h;
public Rectangle(double w, double h) { this.w = w; this.h = h; }
public double area() { return w * h; }
public double perimeter() { return 2 * (w + h); }
}

Expected Output:
Circle area=19.634954084936208 perim=15.707963267948966
Rectangle area=12.0 perim=14.0
THANK - YOU

You might also like