Navigation: Main Tutorial Index | Practice Programs | Previous: Methods | Next: Arrays
Practice: Practice Programs | Practice Program Index
Object-Oriented Programming (OOP) is a programming approach in which we organize programs around objects rather than writing everything as separate instructions.
An object represents something that has:
- State: Data or properties of an object.
- Behavior: Actions that an object can perform.
- Identity: A unique identity that distinguishes one object from another.
For example, consider a Car.
State:
- Brand
- Color
- Model
- Speed
Behavior:
- Start
- Stop
- Accelerate
- Brake
In Java, we use classes to define the structure and behavior of objects.
Imagine developing a banking application. It may contain customers, accounts, transactions, employees, and branches.
Without proper organization, managing all this information and functionality can become difficult.
OOP helps us organize the application into meaningful classes and objects.
- Code Reusability: Existing classes can be reused through inheritance and composition.
- Modularity: Programs can be divided into separate classes.
- Data Security: Encapsulation helps control access to data.
- Maintainability: Changes can be managed more easily.
- Flexibility: Polymorphism allows different implementations through common interfaces.
- Scalability: OOP helps organize large applications.
The four main principles of OOP are:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
A class is a blueprint or template used to create objects.
It defines the properties (variables) and behaviors (methods) that objects of that class can have.
class ClassName {
// Variables
// Constructors
// Methods
}public class Student {
String name;
int age;
void study() {
System.out.println(name + " is studying.");
}
void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Ayush";
s1.age = 22;
s1.displayDetails();
s1.study();
}
}Output:
Name: Ayush
Age: 22
Ayush is studying.
Explanation:
Studentis the class.nameandageare instance variables.study()anddisplayDetails()are methods.s1is a reference variable that refers to the created object.
An object is an instance of a class.
When an object is created, memory is allocated for its instance variables.
ClassName referenceVariable = new ClassName();For example:
Student s1 = new Student();This statement has three parts:
| Part | Meaning |
|---|---|
Student |
Class or reference type |
s1 |
Reference variable |
new Student() |
Creates an object by invoking a constructor |
public class Employee {
String name;
int salary;
public static void main(String[] args) {
Employee e1 = new Employee();
Employee e2 = new Employee();
e1.name = "Rahul";
e1.salary = 30000;
e2.name = "Amit";
e2.salary = 40000;
System.out.println(e1.name + " " + e1.salary);
System.out.println(e2.name + " " + e2.salary);
}
}Output:
Rahul 30000
Amit 40000
Each object has its own copy of instance variables.
| Class | Object |
|---|---|
| Blueprint or template | Instance of a class |
| Defines properties and behaviors | Contains actual instance data |
| Logical definition | Runtime entity |
| One class can create multiple objects | Each object has its own identity |
Encapsulation is the process of combining data and the methods that operate on that data into a single unit (class), while controlling direct access to the internal data.
In Java, encapsulation is commonly implemented using:
- Private instance variables.
- Public getter methods.
- Public setter methods.
Consider a bank account.
If anyone can directly modify the account balance, the application may allow invalid operations.
For example:
account.balance = -50000;This is not desirable.
Encapsulation allows us to control how the balance is accessed and modified.
public class BankAccount {
private String accountHolder;
private double balance;
public void setAccountHolder(String accountHolder) {
this.accountHolder = accountHolder;
}
public String getAccountHolder() {
return accountHolder;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposit successful.");
} else {
System.out.println("Invalid amount.");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawal successful.");
} else {
System.out.println("Invalid amount or insufficient balance.");
}
}
public double getBalance() {
return balance;
}
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.setAccountHolder("Ayush");
account.deposit(5000);
account.withdraw(1000);
System.out.println("Account Holder: " +
account.getAccountHolder());
System.out.println("Balance: " + account.getBalance());
}
}Output:
Deposit successful.
Withdrawal successful.
Account Holder: Ayush
Balance: 4000.0
Getter: A method used to retrieve the value of a private variable.
public double getBalance() {
return balance;
}Setter: A method used to update the value of a private variable, usually with validation.
public void setAge(int age) {
if (age > 0) {
this.age = age;
}
}- Protects internal data from uncontrolled access.
- Allows validation before changing values.
- Makes code easier to maintain.
- Gives control over how data is accessed.
- Reduces unwanted dependencies between classes.
Important: Encapsulation is not simply making every variable private. It is about controlling access to an object's internal state through a well-designed public interface.
Inheritance is an OOP concept in which one class acquires accessible properties and behaviors from another class.
It helps us reuse existing code and create relationships between classes.
For example:
- Animal is a parent class.
- Dog is a child class.
- Dog can use accessible methods inherited from Animal.
| Term | Meaning |
|---|---|
| Parent class | Class whose members are inherited |
| Child class | Class that extends another class |
| Superclass | Another name for parent class |
| Subclass | Another name for child class |
extends |
Keyword used for class inheritance |
class Parent {
// Parent class members
}
class Child extends Parent {
// Child class members
}class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}
public class SingleInheritance {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}Output:
Animal is eating.
Dog is barking.
Explanation:
Animalis the parent class.Dogis the child class.Dog extends Animalestablishes inheritance.- The Dog object can call the accessible
eat()method inherited from Animal.
Inheritance is commonly explained using five structures.
One child class inherits from one parent class.
Animal
|
v
Dog
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}A class inherits from another child class, creating a chain.
Animal
|
v
Mammal
|
v
Dog
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Mammal extends Animal {
void walk() {
System.out.println("Walking...");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("Barking...");
}
}public class MultilevelInheritance {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.walk();
d.bark();
}
}Output:
Eating...
Walking...
Barking...
Multiple child classes inherit from the same parent class.
Animal
/ \
v v
Dog Cat
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing.");
}
}Multiple inheritance means one class inherits from more than one parent class.
Java does not support multiple inheritance through classes.
For example, this is not valid Java:
class A {
}
class B {
}
// Not valid Java
class C extends A, B {
}However, Java supports multiple inheritance of type through interfaces.
Hybrid inheritance combines two or more inheritance structures.
Java does not support hybrid inheritance through classes alone, but it can be achieved using interfaces.
| Inheritance type | Supported in Java? |
|---|---|
| Single | Yes, using classes |
| Multilevel | Yes, using classes |
| Hierarchical | Yes, using classes |
| Multiple | Through interfaces, not multiple classes |
| Hybrid | Can be achieved using interfaces |
The super keyword refers to the immediate parent class object.
It is commonly used to:
- Access parent class variables.
- Call parent class methods.
- Call parent class constructors.
class Parent {
int number = 100;
}
class Child extends Parent {
int number = 200;
void display() {
System.out.println(number);
System.out.println(super.number);
}
}
public class SuperVariable {
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}Output:
200
100
Here:
numberaccesses the child class variable.super.numberaccesses the parent class variable.
class Parent {
void display() {
System.out.println("Parent method.");
}
}
class Child extends Parent {
void display() {
System.out.println("Child method.");
super.display();
}
}
public class SuperMethod {
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}Output:
Child method.
Parent method.
class Parent {
Parent() {
System.out.println("Parent constructor.");
}
}
class Child extends Parent {
Child() {
super();
System.out.println("Child constructor.");
}
}
public class SuperConstructor {
public static void main(String[] args) {
Child obj = new Child();
}
}Output:
Parent constructor.
Child constructor.
Important Rules:
super()calls a parent class constructor.- An explicit
super()call must be the first statement in a constructor. - If no explicit constructor invocation is written, Java normally inserts an implicit
super()call when applicable. - A child constructor cannot use both
this()andsuper()as explicit constructor invocations.
Polymorphism means "many forms."
In Java, polymorphism allows the same method name or reference type to represent different behaviors depending on the context.
For example, a method named sound() can behave differently for a Dog, Cat, and Cow.
Java supports two commonly discussed forms of polymorphism:
- Compile-time polymorphism.
- Runtime polymorphism.
Compile-time polymorphism is commonly achieved through method overloading.
The compiler determines which overloaded method should be called based on the arguments.
public class CompileTimePolymorphism {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
CompileTimePolymorphism obj =
new CompileTimePolymorphism();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
}
}Output:
30
60
Runtime polymorphism is achieved through method overriding.
When a subclass provides its own implementation of an inherited instance method, the JVM determines which implementation to execute based on the actual object.
class Animal {
void sound() {
System.out.println("Animal makes a sound.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks.");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows.");
}
}
public class RuntimePolymorphism {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound();
a = new Cat();
a.sound();
}
}Output:
Dog barks.
Cat meows.
Animal a = new Dog();Here:
Animalis the reference type.Dogis the actual object type.- The reference can access members available through the Animal type.
- The overridden instance method implementation is selected according to the actual object.
This behavior is called dynamic method dispatch.
| Method Overloading | Method Overriding |
|---|---|
| Same method name with different parameter lists | Subclass provides a new implementation of an inherited method |
| Usually happens within the same class | Happens between parent and child classes |
| Compile-time polymorphism | Runtime polymorphism |
| Return type alone cannot distinguish overloaded methods | Return type must be compatible with the overridden method |
| Inheritance is not required | Inheritance is required |
- The method must be inherited and eligible for overriding.
- The method name and parameter list must match.
- The return type must be the same or covariant.
- Access visibility cannot be reduced.
- A final method cannot be overridden.
- A private method is not overridden because it is not inherited.
- Static methods are hidden, not overridden in the runtime-polymorphism sense.
- The
@Overrideannotation helps the compiler verify the intended override.
Abstraction means hiding unnecessary implementation details and exposing only the essential functionality.
Consider an ATM machine.
When you withdraw money, you interact with buttons and the screen. You do not need to understand all the internal banking operations.
The ATM provides a simple interface while hiding its internal implementation.
Java provides two major ways to achieve abstraction:
- Abstract classes.
- Interfaces.
An abstract class is a class declared using the abstract keyword.
It cannot be instantiated directly.
It can contain:
- Abstract methods.
- Concrete methods.
- Instance variables.
- Constructors.
- Static methods.
abstract class ClassName {
abstract void methodName();
void display() {
System.out.println("Concrete method.");
}
}abstract class Animal {
abstract void sound();
void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks.");
}
}
public class AbstractClassExample {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
d.eat();
}
}Output:
Dog barks.
Animal is eating.
Explanation:
Animalis an abstract class.sound()is an abstract method.eat()is a concrete method.Dogprovides an implementation ofsound().- We cannot create an object directly using
new Animal().
An interface is a reference type that defines a contract for classes.
A class implements an interface and provides implementations for its abstract methods.
interface InterfaceName {
void methodName();
}
class ClassName implements InterfaceName {
public void methodName() {
// Implementation
}
}interface Vehicle {
void start();
void stop();
}
class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car started.");
}
@Override
public void stop() {
System.out.println("Car stopped.");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Car car = new Car();
car.start();
car.stop();
}
}Output:
Car started.
Car stopped.
An interface can contain:
- Abstract methods.
- Default methods.
- Static methods.
- Constants.
Interface fields are implicitly public static final.
Methods declared without private, static, or default modifiers are implicitly public and abstract.
A default method contains an implementation and is declared using the default keyword.
interface Vehicle {
void start();
default void fuelType() {
System.out.println("Vehicle fuel information.");
}
}
class Car implements Vehicle {
public void start() {
System.out.println("Car started.");
}
}
public class DefaultMethodExample {
public static void main(String[] args) {
Car car = new Car();
car.start();
car.fuelType();
}
}Output:
Car started.
Vehicle fuel information.
Java 8 also allows static methods inside interfaces.
interface Calculator {
static int add(int a, int b) {
return a + b;
}
}
public class InterfaceStaticMethod {
public static void main(String[] args) {
System.out.println(Calculator.add(10, 20));
}
}Output:
30
The static method is called using the interface name.
| Abstract Class | Interface |
|---|---|
Declared using abstract class |
Declared using interface |
| A class extends an abstract class | A class implements an interface |
| Can have instance variables | Fields are implicitly public static final |
| Can have constructors | Cannot have constructors |
| Can contain abstract and concrete methods | Java 8 supports abstract, default, and static methods |
| A class can extend only one class | A class can implement multiple interfaces |
| Can maintain instance state | Does not have per-object instance fields |
Use an abstract class when related classes share common state, constructors, and implementation.
Example: Animal, Vehicle, Employee.
Use an interface when you want to define a capability or contract that different classes can implement.
Example: Runnable, Comparable, Serializable.
Java does not allow a class to extend multiple classes, but it allows a class to implement multiple interfaces.
interface Camera {
void takePhoto();
}
interface MusicPlayer {
void playMusic();
}
class Smartphone implements Camera, MusicPlayer {
public void takePhoto() {
System.out.println("Taking photo.");
}
public void playMusic() {
System.out.println("Playing music.");
}
}
public class MultipleInheritance {
public static void main(String[] args) {
Smartphone phone = new Smartphone();
phone.takePhoto();
phone.playMusic();
}
}Output:
Taking photo.
Playing music.
This demonstrates multiple inheritance of type through interfaces.
If a class implements two interfaces containing conflicting default methods with the same signature, the class must resolve the conflict by overriding that method.
interface A {
default void display() {
System.out.println("Interface A");
}
}
interface B {
default void display() {
System.out.println("Interface B");
}
}
class C implements A, B {
@Override
public void display() {
A.super.display();
B.super.display();
}
}These concepts describe relationships between objects.
Association represents a relationship between two independent objects.
Example: A teacher teaches students.
A teacher and a student can exist independently.
class Teacher {
String name;
Teacher(String name) {
this.name = name;
}
}
class Student {
String name;
Student(String name) {
this.name = name;
}
}
public class AssociationExample {
public static void main(String[] args) {
Teacher teacher = new Teacher("Mr. Sharma");
Student student = new Student("Ayush");
System.out.println(teacher.name + " teaches " + student.name);
}
}Output:
Mr. Sharma teaches Ayush
Aggregation is a specialized form of association representing a weak whole-part relationship.
The contained object can exist independently of the container.
Example: A Department has Teachers.
Teachers can exist even if the Department object is removed.
class Teacher {
String name;
Teacher(String name) {
this.name = name;
}
}
class Department {
String departmentName;
Teacher teacher;
Department(String departmentName, Teacher teacher) {
this.departmentName = departmentName;
this.teacher = teacher;
}
void display() {
System.out.println(departmentName);
System.out.println(teacher.name);
}
}
public class AggregationExample {
public static void main(String[] args) {
Teacher t1 = new Teacher("Mr. Sharma");
Department d1 = new Department("Computer Science", t1);
d1.display();
}
}Output:
Computer Science
Mr. Sharma
Composition is a stronger whole-part relationship.
The containing object controls the lifecycle of its contained parts as part of its design.
Example: A House contains Rooms.
class Room {
void display() {
System.out.println("Room exists inside the house.");
}
}
class House {
private Room room;
House() {
room = new Room();
}
void display() {
room.display();
}
}
public class CompositionExample {
public static void main(String[] args) {
House house = new House();
house.display();
}
}Output:
Room exists inside the house.
| Association | Aggregation | Composition |
|---|---|---|
| General relationship | Weak whole-part relationship | Strong whole-part relationship |
| Objects are independent | Part can exist independently | Part lifecycle is controlled by the whole |
| Example: Teacher and Student | Department and Teacher | House and Room |
Important: These relationships describe design and ownership. Java does not automatically enforce aggregation or composition through special keywords.
Access modifiers control the visibility of classes, variables, constructors, and methods.
Java provides four access levels:
publicprotected- Default (package-private)
private
| Access modifier | Same class | Same package | Subclass in another package | Other package |
|---|---|---|---|---|
public |
Yes | Yes | Yes | Yes |
protected |
Yes | Yes | Yes, through inheritance rules | No, except eligible subclass access |
| Default | Yes | Yes | No | No |
private |
Yes | No | No | No |
public class AccessModifierExample {
public int a = 10;
protected int b = 20;
int c = 30;
private int d = 40;
public void display() {
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
public static void main(String[] args) {
AccessModifierExample obj =
new AccessModifierExample();
obj.display();
}
}Output:
10
20
30
40
Note: A top-level class can be declared public or package-private, not private or protected.
The final keyword is used to restrict modification.
It can be applied to variables, methods, and classes.
A final variable can be assigned only once.
public class FinalVariable {
public static void main(String[] args) {
final int MAX = 100;
System.out.println(MAX);
}
}Output:
100
Trying to assign another value to MAX will cause a compilation error.
A final method cannot be overridden by a subclass.
class Parent {
final void display() {
System.out.println("Parent method.");
}
}
class Child extends Parent {
// Cannot override display()
}A final class cannot be extended.
final class Vehicle {
void start() {
System.out.println("Vehicle started.");
}
}
// Cannot extend Vehicle| Keyword | Purpose |
|---|---|
final |
Restricts reassignment, overriding, or inheritance depending on usage |
finally |
Exception-handling block that normally executes after try/catch processing |
finalize() |
Deprecated object-finalization method in modern Java; not a reliable resource-management mechanism |
This example combines classes, objects, encapsulation, inheritance, and method overriding.
class Employee {
private int id;
private String name;
protected double salary;
Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public void displayDetails() {
System.out.println("Employee ID: " + id);
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
}
public double calculateBonus() {
return salary * 0.05;
}
}
class Manager extends Employee {
Manager(int id, String name, double salary) {
super(id, name, salary);
}
@Override
public double calculateBonus() {
return salary * 0.15;
}
}
public class EmployeeManagement {
public static void main(String[] args) {
Employee e1 = new Employee(101, "Rahul", 30000);
Manager m1 = new Manager(102, "Ayush", 50000);
e1.displayDetails();
System.out.println("Bonus: " + e1.calculateBonus());
System.out.println();
m1.displayDetails();
System.out.println("Bonus: " + m1.calculateBonus());
}
}Output:
Employee ID: 101
Name: Rahul
Salary: 30000.0
Bonus: 1500.0
Employee ID: 102
Name: Ayush
Salary: 50000.0
Bonus: 7500.0
- Class: Employee and Manager.
- Object: e1 and m1.
- Encapsulation: Private employee ID and name.
- Inheritance: Manager extends Employee.
- Polymorphism: Manager overrides calculateBonus().
- Abstraction: This example does not use an abstract class or interface.
| Mistake | Explanation |
|---|---|
| Confusing class and object | A class is a blueprint; an object is an instance |
| Making all fields public | Can expose internal state without control |
| Assuming Java supports multiple class inheritance | A class can extend only one class |
| Confusing overloading and overriding | Overloading changes parameter lists; overriding redefines an inherited method |
| Trying to instantiate an abstract class | Abstract classes cannot be directly instantiated |
| Forgetting public when implementing interface methods | Implementations of public interface methods must be public |
Using super() after another constructor statement |
An explicit super() invocation must be first |
| Assuming private methods are overridden | Private methods are not inherited |
| Assuming static methods are overridden | Static methods are hidden |
| Confusing aggregation and composition | Their difference concerns ownership and lifecycle |
Q1. What is OOP in Java?
Answer: OOP is a programming approach that organizes programs around classes and objects.
Q2. What is a class?
Answer: A class is a blueprint that defines the properties and behaviors of objects.
Q3. What is an object?
Answer: An object is an instance of a class that has state, behavior, and identity.
Q4. What are the four pillars of OOP?
Answer: Encapsulation, inheritance, polymorphism, and abstraction.
Q5. What is encapsulation?
Answer: Encapsulation combines data and related methods into a class and controls access to internal data.
Q6. What is inheritance?
Answer: Inheritance allows a subclass to acquire accessible members from a superclass.
Q7. What is polymorphism?
Answer: Polymorphism allows one interface or method name to represent different behaviors.
Q8. What is abstraction?
Answer: Abstraction hides implementation details and exposes essential functionality.
Q9. What is the difference between an abstract class and an interface?
Answer: An abstract class can maintain instance state and constructors. An interface defines a contract and, in Java 8, supports abstract, default, and static methods.
Q10. Does Java support multiple inheritance?
Answer: Java does not support multiple inheritance through classes, but a class can implement multiple interfaces.
Q11. What is method overriding?
Answer: Method overriding occurs when a subclass provides a compatible implementation of an inherited instance method.
Q12. What is dynamic method dispatch?
Answer: It is the mechanism through which an overridden instance method is selected at runtime based on the actual object.
Q13. What is the use of super?
Answer: It is used to access parent class members and invoke a parent class constructor.
Q14. What is the use of this?
Answer: It refers to the current object and can be used to access its members or invoke another constructor.
Q15. Can an abstract class have a constructor?
Answer: Yes. Its constructor is invoked as part of subclass object construction.
Q16. Can an interface have a constructor?
Answer: No. Interfaces cannot be instantiated and do not have constructors.
Q17. Can a final class be inherited?
Answer: No. A final class cannot be extended.
Q18. What is the difference between aggregation and composition?
Answer: Aggregation is a weak whole-part relationship in which parts can exist independently. Composition represents stronger ownership and lifecycle dependency.
Q19. Can a constructor be overridden?
Answer: No. Constructors are not inherited, so they cannot be overridden.
Q20. Why is encapsulation important?
Answer: It protects internal data and provides controlled access through methods.
Instructions: Solve these questions independently. Do not skip the practical exercises. Create separate Java files and test your programs with different inputs.
- Create a Student class with name, roll number, and age. Create two objects and display their details.
- Create a Car class with brand, model, and color. Add methods to start and stop the car.
- Create an Employee class with employee ID, name, and salary.
- Create a Rectangle class with methods to calculate area and perimeter.
- Create a BankAccount class with private account number and balance. Implement getter and setter methods with suitable validation.
- Create a Person class with a parameterized constructor to initialize name and age.
- Create a class demonstrating the use of the
thiskeyword. - Create a class demonstrating the difference between static and instance variables.
- Create a class demonstrating constructor overloading.
- Create a class demonstrating the use of the
finalkeyword.
- Create a program demonstrating single inheritance using Animal and Dog classes.
- Create a program demonstrating multilevel inheritance using Person, Employee, and Manager classes.
- Create a program demonstrating hierarchical inheritance using Vehicle, Car, and Bike classes.
- Create a program demonstrating method overriding using a parent and child class.
- Create a program demonstrating runtime polymorphism using Animal, Dog, and Cat classes.
- Create a Calculator class demonstrating compile-time polymorphism through method overloading.
- Create an abstract Shape class with abstract methods for calculating area and perimeter. Implement Circle and Rectangle subclasses.
- Create an interface Payment with a payment method. Implement it using CreditCardPayment and UPIPayment classes.
- Create a program demonstrating multiple inheritance through interfaces.
- Create a program demonstrating the use of the
superkeyword for variables, methods, and constructors.
- Design a Banking Management System using encapsulation, inheritance, and polymorphism.
- Design an Employee Management System with different employee types and salary calculations.
- Create a Library Management System using classes for Book, Member, and Library.
- Create a Vehicle Rental System using abstraction and inheritance.
- Create an Online Shopping System using Product, Customer, Cart, and Order classes.
- Create a Hospital Management System using inheritance and encapsulation.
- Create a Payment Gateway design using interfaces and runtime polymorphism.
- Demonstrate association, aggregation, and composition using separate Java programs.
- Design a Shape Management System that calculates areas of different shapes using runtime polymorphism.
- Build a complete Student Management System using the four pillars of OOP.
After completing this chapter, you should understand:
- What object-oriented programming is and why Java uses it.
- The difference between classes and objects.
- How encapsulation protects internal data.
- How inheritance supports code reuse.
- The different inheritance structures supported by Java.
- How to use
thisandsuper. - The difference between method overloading and overriding.
- Compile-time and runtime polymorphism.
- How abstract classes and interfaces work.
- Java 8 interface features, including default and static methods.
- The difference between association, aggregation, and composition.
- Access modifiers and the
finalkeyword. - How to design practical applications using OOP concepts.
Chapter 7: Java Arrays
Topics to explore:
- Introduction to Arrays.
- Declaration and Initialization.
- One-Dimensional Arrays.
- Two-Dimensional Arrays.
- Multidimensional Arrays.
- Array Traversal.
- Searching and Sorting.
- Common Array Programs.
- Arrays Interview Questions.
- Practice Exercises.
Learning Tip: Don't just memorize the four pillars of OOP. Try to identify where each principle is used in real applications such as banking systems, shopping apps, employee management systems, and payment gateways.