0% found this document useful (0 votes)
2 views6 pages

Java Multiple Inheritance

The document provides a comprehensive overview of Java's multiple inheritance using interfaces, including examples of how to implement multiple interfaces, handle polymorphism, and resolve conflicts with default methods. It also covers the use of static methods in interfaces and presents an advanced mini project demonstrating real-world application of multiple interfaces. Key points emphasize that Java achieves multiple inheritance through interfaces rather than classes.
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)
2 views6 pages

Java Multiple Inheritance

The document provides a comprehensive overview of Java's multiple inheritance using interfaces, including examples of how to implement multiple interfaces, handle polymorphism, and resolve conflicts with default methods. It also covers the use of static methods in interfaces and presents an advanced mini project demonstrating real-world application of multiple interfaces. Key points emphasize that Java achieves multiple inheritance through interfaces rather than classes.
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

Java Multiple Inheritance (Interfaces) – Full

Assignment Solutions

Q1) Basic Multiple Inheritance


Question: Flyable + Swimmable → Duck

interface Flyable {
void fly();
}

interface Swimmable {
void swim();
}

class Duck implements Flyable, Swimmable {


public void fly() {
[Link]("Duck is flying");
}

public void swim() {


[Link]("Duck is swimming");
}
}

class Main {
public static void main(String[] args) {
Duck d = new Duck();
[Link]();
[Link]();
}
}

Output

Duck is flying
Duck is swimming

Explanation: One class implements multiple interfaces.

Viva Points: - Java supports multiple inheritance via interfaces

1
Q2) Multiple Inheritance with Polymorphism

interface Payment {
void pay(double amount);
}

interface Refund {
void refund(double amount);
}

class UPITransaction implements Payment, Refund {


public void pay(double amount) {
[Link]("Paid: " + amount);
}

public void refund(double amount) {


[Link]("Refunded: " + amount);
}
}

class Main {
public static void main(String[] args) {
Payment p = new UPITransaction();
[Link](1000);

Refund r = new UPITransaction();


[Link](500);
}
}

Output

Paid: 1000.0
Refunded: 500.0

Explanation: Same object accessed via different interface references.

Viva Points: - Interface reference enables polymorphism

Q3) Interface Static Method

interface Validator {
static boolean isValidPin(String pin) {
if ([Link]() != 4) return false;

for (char c : [Link]()) {

2
if (![Link](c)) return false;
}
return true;
}
}

class Main {
public static void main(String[] args) {
String[] pins = {"1234", "12a4", "999", "5678"};

for (String p : pins) {


if ([Link](p))
[Link](p + " Valid");
else
[Link](p + " Invalid");
}
}
}

Output

1234 Valid
12a4 Invalid
999 Invalid
5678 Valid

Explanation: Static method belongs to interface, called using interface name.

Viva Points: - Cannot call static method using object

Q4) Conflict Resolution (Default Methods)

interface Camera {
default void start() {
[Link]("Camera starting...");
}

default void stop() {


[Link]("Camera stopping...");
}
}

interface MusicPlayer {
default void start() {
[Link]("Music player starting...");
}

3
default void stop() {
[Link]("Music player stopping...");
}
}

class SmartPhone implements Camera, MusicPlayer {


public void start() {
[Link]("SmartPhone is starting all modules...");
[Link]();
[Link]();
}

public void stop() {


[Link]("SmartPhone is stopping all modules...");
[Link]();
[Link]();
}
}

class Main {
public static void main(String[] args) {
SmartPhone s = new SmartPhone();
[Link]();
[Link]();
}
}

Output

SmartPhone is starting all modules...


Camera starting...
Music player starting...
SmartPhone is stopping all modules...
Camera stopping...
Music player stopping...

Explanation: Conflict resolved by overriding.

Viva Points: - Must override when same default method exists

Q5) Advanced Mini Project (AdminPanel)

interface LoginService {
boolean login(String user, String pass);
}

interface NotificationService {

4
void sendEmail(String msg);
void sendSMS(String msg);
}

interface ReportService {
void generateReport();
}

class AdminPanel implements LoginService, NotificationService, ReportService


{
public boolean login(String user, String pass) {
return [Link]("admin") && [Link]("1234");
}

public void sendEmail(String msg) {


[Link]("Email sent: " + msg);
}

public void sendSMS(String msg) {


[Link]("SMS sent: " + msg);
}

public void generateReport() {


[Link]("Report generated");
}
}

class Main {
public static void main(String[] args) {
AdminPanel admin = new AdminPanel();

// SUCCESS CASE
if ([Link]("admin", "1234")) {
[Link]();
[Link]("Report ready");
[Link]("Report ready");
} else {
[Link]("Login Failed");
}

// FAILURE CASE
if (![Link]("user", "0000")) {
[Link]("Login Failed");
}
}
}

Output

5
Report generated
Email sent: Report ready
SMS sent: Report ready
Login Failed

Explanation: Combines multiple interfaces into real-world system.

Viva Points: - Multiple interface implementation - Real-world abstraction

One-Line Rule
Java achieves multiple inheritance using interfaces, not classes.

End of Document

You might also like