OBJECT ORIENTED PROGRAMMING WITH JAVA - DETAILED
ASSIGNMENT
SECTION A
1(a) Output Explanation:
Class B has x = 20 and inherits x = 10 from class A. Using super.x accesses parent variable.
[Link](x + super.x); // 20 + 10 = 30
Output: 30
1(b) Statement Analysis:
The statement is incorrect. Java does not support multiple inheritance using classes but supports it
using interfaces. A class can implement multiple interfaces, thus inheriting behavior from multiple
sources.
1(c) Difference between Overloading and Overriding:
Overloading occurs in the same class with different parameters and is resolved at compile time.
Overriding occurs in inheritance when a subclass provides a specific implementation of a method
and is resolved at runtime.
1(d) Thread vs Runnable:
Thread is a class used to create threads directly, while Runnable is an interface that provides better
flexibility and allows extending other classes.
1(e) Static Method:
Static methods can be called without creating an object of the class.
1(f) Functional Interface:
A functional interface contains exactly one abstract method and is used in lambda expressions.
SECTION B
2(a) Abstract Class Bank Example:
abstract class Bank {
abstract int getBalance();
}
class BankA extends Bank {
int getBalance() { return 100; }
}
class BankB extends Bank {
int getBalance() { return 150; }
}
class BankC extends Bank {
int getBalance() { return 200; }
}
2(a)(ii) Overloading Issue:
Ambiguous method calls may occur when parameter types are similar. This leads to compile-time
errors.
2(b) Thread Lifecycle:
New → Runnable → Running → Waiting → Terminated
Factorial using Runnable:
class Factorial implements Runnable {
public void run() {
int fact = 1;
for(int i=1;i<=5;i++) fact*=i;
[Link](fact);
}
}
2(c) Multithreading Issues:
Race conditions occur due to unsynchronized access to shared resources.
2(d) Multiple Inheritance:
Java does not allow multiple inheritance using classes to avoid ambiguity.
SECTION C
3(a) Shape Inheritance Example:
class Shape {
void printShape() { [Link]("This is shape"); }
}
class Rectangle extends Shape {
void printRectangle() { [Link]("This is rectangular shape"); }
}
class Square extends Rectangle {}
3(b) Output:
Concrete method of parent class
[Link] pauses execution for specified milliseconds.
SECTION D
4(a) Concepts:
Encapsulation hides data, Polymorphism allows multiple behavior, Abstraction hides
implementation details.
4(b) Banking Issue:
Race condition occurs when two threads access shared data. It is solved using synchronization.