Skip to content

Commit a8f7a24

Browse files
committed
Add multiple OOP-based implementations for average calculation.
- Implemented average calculation using basic multi-level class inheritance. > DemoA → DemoB → DemoC → DemoD > Variables x and y initialized in parent classes > calcAverage() in DemoC computes and prints the average - Created an abstract class-based version. > Abstract class DemoBase holds variable x > Inherited through DemoB and DemoC, where calcAverage() is defined > DemoD extends DemoC and invokes the inherited logic - Developed an interface-based implementation. > Defined AverageCalculator interface with method calcAverage() > DemoA implements the interface with a default message > Extended by DemoB and DemoC; DemoD overrides calcAverage() to compute average - All examples demonstrate key OOP concepts: inheritance, abstraction, and polymorphism. - Implement average calculation using: 1. Basic multi-level class inheritance 2. Abstract class as a base with derived classes 3. Interface-based implementation with method overriding - Demonstrates polymorphism and inheritance in different OOP styles. Signed-off-by: Someshdiwan <Someshdiwan369@gmail.com>
1 parent 5f9568e commit a8f7a24

3 files changed

Lines changed: 109 additions & 0 deletions

File tree

Section12Inheritance/src/Demo.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class DemoA
2+
{
3+
int x = 5;
4+
}
5+
class DemoB extends DemoA
6+
{
7+
int y = 7;
8+
}
9+
class DemoC extends DemoB
10+
{
11+
double average = 0.0;
12+
void calcAverage()
13+
{
14+
average = (x + y) / 2.0;
15+
System.out.println(average);
16+
}
17+
}
18+
class DemoD extends DemoC
19+
{
20+
// Additional functionality can be added in DemoD if needed
21+
}
22+
class DemoE
23+
{
24+
public static void main(String[] args)
25+
{
26+
DemoD objD = new DemoD();
27+
objD.calcAverage();
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
abstract class DemoBase
2+
{
3+
int x = 5;
4+
}
5+
class DemoB extends DemoBase
6+
{
7+
int y = 7;
8+
}
9+
class DemoC extends DemoB
10+
{
11+
double average = 0.0;
12+
void calcAverage()
13+
{
14+
average = (x + y) / 2.0;
15+
System.out.println("Average: " + average);
16+
}
17+
}
18+
class DemoD extends DemoC
19+
{
20+
21+
}
22+
class Demo
23+
{
24+
public static void main(String[] args)
25+
{
26+
DemoD objD = new DemoD();
27+
objD.calcAverage();
28+
}
29+
}

Section14Interfaces/src/Demo.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
interface AverageCalculator
2+
{
3+
void calcAverage();
4+
}
5+
6+
class DemoA implements AverageCalculator
7+
{
8+
int x = 5; //initialize the value of x is 5.
9+
10+
@Override
11+
public void calcAverage() //Method for calculating Average.
12+
{
13+
System.out.println("Calculating average in DemoA");
14+
}
15+
}
16+
17+
class DemoB extends DemoA
18+
{
19+
int y = 7;
20+
}
21+
22+
class DemoC extends DemoB
23+
{
24+
25+
}
26+
class DemoD extends DemoC implements AverageCalculator
27+
{
28+
double average = 0.0;
29+
@Override
30+
public void calcAverage()
31+
{
32+
average = (x + y) / 2.0;
33+
System.out.println("Average: " + average);
34+
}
35+
}
36+
37+
class Demo
38+
{
39+
public static void main(String[] args)
40+
{
41+
DemoD objD = new DemoD();
42+
objD.calcAverage();
43+
}
44+
}
45+
/*
46+
AverageCalculator interface with a method calcAverage().
47+
DemoA implements it and gives a basic print message.
48+
DemoB and DemoC extend the hierarchy (inherit x and y).
49+
DemoD extends DemoC and overrides calcAverage() to calculate the average of x and y.
50+
In main(), an object of DemoD is created and calcAverage() is called.
51+
*/

0 commit comments

Comments
 (0)