Inheritance, Polymorphism and Interfaces – Examples (Java)

Page 4 of 4 · Object-oriented programming – examples

Tasks in which several classes share common properties: a base class, derived classes that extend it and change it (@Override), abstract classes and interfaces.

The basic idea of inheritance:

abstract class Shape {                 // an abstract class – new Shape() is not possible
    abstract double area();            // every derived class MUST write it
}

class Circle extends Shape {           // a Circle IS a Shape
    double r;
    Circle(double r) { this.r = r; }
    @Override
    double area() { return r * r * Math.PI; }
}

Shape s = new Circle(2);               // a variable of type Shape, an object of type Circle
System.out.println(s.area());          // the method from the class Circle is called (polymorphism)
Before you start: read the lessons Class inheritance, Abstract classes and interfaces.
How to practise: write the class and the program on your own, run it on the example from the task, and only then open the hint, and finally the solution. All solutions are in Java; the helper classes are in the same file (without the word public), so the program runs as one file.
TASK 01Difficulty: medium

Geometric shapes

Make an abstract class Shape with the abstract methods area() and name(), and the derived classes Circle, Rectangle and Triangle. Read n shapes (the marks C r, R a b or T a h), keep them in one array of type Shape[], and print the area of each one and the total area.

Example

Input

3
C 1
R 2 3
T 4 5

Output

Circle: 3.14
Rectangle: 6.00
Triangle: 10.00
Total: 19.14
TASK 02Difficulty: medium

A worker and a boss (super)

The class Worker has a name and a base salary, and a method salary(). The class Boss extends Worker also has the number of people in the team, and the boss's salary is the base salary + 2000 dinars for every team member. Make the constructors (the boss calls super) and print the salaries of one worker and one boss.

Example

Input

Pera 60000
Mara 80000 5

Output

Pera: 60000
Mara (head of a team of 5): 90000
TASK 03Difficulty: easy

Animals in a zoo

The base class Animal has a name and a method sound() that returns "…". The classes Dog, Cat and Cow override the method sound(). Create a few animals in a list ArrayList<Animal> and for each one print its name and sound.

Example

Output

Rex says: Woof!
Kitty says: Meow!
Milka says: Moo!
Nemo the fish says: ...
TASK 04Difficulty: harder

An interface: rechargeable devices

Make an interface Rechargeable with the methods charge(int minutes) and battery() (a percentage 0–100). The class Phone charges 2% per minute, and the class Laptop 1% per minute. The battery cannot go over 100%. For a phone at 35% and a laptop at 50% read the number of minutes of charging and print the state of the batteries.

Example

Input

40

Output

Phone: 100%
Laptop: 90%
TASK 05Difficulty: harder

Comparable: sorting athletes

The class Athlete (name, result in seconds in the 100 m) implements the interface Comparable<Athlete> so that the "smaller" one is the one with the better (smaller) time. Read n athletes, sort them with Collections.sort and print the ranking with medals for the first three places.

Example

Input

4
Luka 12.40
Nina 11.95
Petar 12.10
Sara 12.85

Output

gold: Nina 11.95
silver: Petar 12.1
bronze: Luka 12.4
4. Sara 12.85
Common mistakes and tips
  • A derived class inherits the attributes and methods, and it calls the constructor of the base class with super(...) as the first statement.
  • @Override is not required, but it is useful: the compiler reports an error if the name of the method does not match a method of the base class.
  • Polymorphism: an array of type Shape[] can contain both circles and rectangles – each one answers the same call "in its own way".
  • A class inherits only one class (extends), but it can implement several interfaces (implements A, B).

Check your knowledge

Open the quiz

Mini quiz: inheritance

1. With which word does a class inherit another class?




2. What does it mean that a class is abstract?




3. The variable Shape s = new Circle(2); calls s.area(). Which method is executed?