Create a Java program that demonstrates the use of abstract classes and interfaces, themed around a jungle council of animals selecting a leader.
The program will involve an abstract class `Animal`, representing the general characteristics of an animal, and an interface `CouncilMember`, representing the ability to participate in the council and cast a vote for a leader.
### Java Program: Jungle Council
#### Part 1: Abstract Class `Animal`
abstract class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
// Abstract method
abstract void makeSound();
}
```
In this part, `Animal` is an abstract class with a property `name` and an abstract method `makeSound()`.
Each specific animal will have its own implementation of `makeSound()`.
#### Part 2: Interface `CouncilMember`
```java
interface CouncilMember {
void castVote();
}
The `CouncilMember` interface contains a method `castVote()`, which will be implemented by animals that are members of the council.
#### Part 3: Specific Animal Classes
```java
class Lion extends Animal implements CouncilMember {
public Lion(String name) {
super(name);
}
@Override
void makeSound() {
System.out.println(name + " roars!");
}
@Override
public void castVote() {
System.out.println(name + " votes!");
}
}
class Elephant extends Animal implements CouncilMember {
public Elephant(String name) {
super(name);
}
@Override
void makeSound() {
System.out.println(name + " trumpets!");
}
@Override
public void castVote() {
System.out.println(name + " votes!");
}
}
```
In this part, `Lion` and `Elephant` classes extend the `Animal` class and implement the `CouncilMember` interface, providing their own implementations of `makeSound()` and `castVote()`.
#### Part 4: Jungle Council Meeting
```java
public class JungleCouncilMeeting {
public static void main(String[] args) {
Lion leo = new Lion("Leo");
Elephant ella = new Elephant("Ella");
// Animals make sounds
leo.makeSound();
ella.makeSound();
// Council members casting votes
leo.castVote();
ella.castVote();
// Determine the leader (simplified example)
System.out.println("The council has voted. The new leader is " + leo.getName() + "!");
}
}
```
In this part, a `main` method simulates a council meeting. The lion and elephant make their sounds and then cast their votes. Finally, a simple statement announces the new leader.
### Explanation
- The abstract class `Animal` provides a template for all animals, enforcing a common structure.
- The interface `CouncilMember` defines behavior specific to council members, namely the ability to cast a vote.
- By extending `Animal` and implementing `CouncilMember`, the `Lion` and `Elephant` classes inherit properties and methods from `Animal` and also provide specific implementations for the methods in `CouncilMember`.
- The `main` method in `JungleCouncilMeeting` demonstrates polymorphism and how objects of these classes can interact.
This program effectively teaches students the concept and application of abstract classes and interfaces in Java, using a fun and engaging jungle-themed example.