Code Logo

Student Route Card with Inheritance

Published at22 Apr 2026
Java OOP Easy 0 views
Like0

A school transport desk prints route cards for students who ride the morning shuttle. Every card needs the student's name and the route name, and the final printed format should read "Student Name - Route RouteName".

Your task is to model this with inheritance. A base class should represent the shared card owner data, while a child class should represent the student route card and produce the final label.

For example, if the input is "Ayu" and "Blue", the result should be "Student Ayu - Route Blue". If the input is "Bima" and "North", the result should be "Student Bima - Route North".

This challenge is not about difficult logic. It is about using a parent class for shared data and a child class for the student-specific card behavior. The returned string should always follow the same fixed format.

Example Input & Output

Example 1
Input
name = "Cici", route = "East"
Output
"Student Cici - Route East"
Explanation

The route card format stays the same for every student.

Example 2
Input
name = "Ayu", route = "Blue"
Output
"Student Ayu - Route Blue"
Explanation

The child class combines the inherited name with its own route field.

Example 3
Input
name = "Bima", route = "North"
Output
"Student Bima - Route North"
Explanation

Any route name should appear in the same fixed label format.

Algorithm Flow

Recommendation Algorithm Flow for Student Route Card with Inheritance
Recommendation Algorithm Flow for Student Route Card with Inheritance

Solution Approach

This is a gentle inheritance exercise. The main OOP idea is that the child class should reuse data that already belongs in the parent class instead of redefining it from scratch.

A clean design is to create a base class such as RouteCard that stores the owner's name. Then a child class like StudentRouteCard can extend that base class, add the route field, and expose a method that returns the final printed label.

The relationship looks like this in Java terms:

class RouteCard {
    protected String name;

    public RouteCard(String name) {
        this.name = name;
    }
}

class StudentRouteCard extends RouteCard {
    private String route;

    public StudentRouteCard(String name, String route) {
        super(name);
        this.route = route;
    }
}

The important inheritance step is extends RouteCard together with super(name). That tells Java that the student route card should reuse the parent class's name-handling logic instead of creating a separate disconnected field setup.

After construction, the child class can build the result with a method like getLabel() that uses both the inherited name and the child-specific route. That is the core learning goal here: parent and child classes each handle the part of the object they are responsible for.

The runtime is O(1). The value of the problem is not algorithmic complexity, but learning how inheritance lets one class build on another in a natural Java style.

Best Answers

java - Approach 1
class RouteCard {
    protected String name;

    public RouteCard(String name) {
        this.name = name;
    }
}

class StudentRouteCard extends RouteCard {
    private String route;

    public StudentRouteCard(String name, String route) {
        super(name);
        this.route = route;
    }

    public String getLabel() {
        return String.format("Student %s - Route %s", name, route);
    }
}

class Solution {
    public static String makeRouteCard(String name, String route) {
        return new StudentRouteCard(name, route).getLabel();
    }
}