Question: I want a program to take input from user and build a new class that inherits existing parent class and its functions. is it possible

I want a program to take input from user and build a new class that inherits existing parent class and its functions. is it possible to do so, if yes how do i do that? (Please include a short code).

i am expecting the program to build a new .java class file with the name entered by the user

 

I have got a parent class Client which needs to be inherited by class name entered by user.

public abstract class Client {

    protected final int id;
    protected final String name;
    protected double discountedAmount;

    public Client(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

Also I have got a AddClient class which implements Driver class that runs the program

import java.util.Scanner;
public class AddClient implements Driver {

    @Override
    public void welcome() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Do you want to add new Client Tier?");
        String answer = scanner.next();
        if (answer.equals("Yes")) {
                System.out.println("Enter new client type:");
                String newClient = scanner.next();
                System.out.println("Enter Discount percent:");
                double discount = scanner.nextDouble();
                System.out.println("The new Client is" + " " + newClient + " " + "with a discount of" + " " + discount + "%");

            }
            else if (answer.equals("yes")) {
                System.out.println("Enter new client type:");
                String newClient = scanner.next();
                System.out.println("Enter Discount percent:");
                double discount = scanner.nextDouble();
                System.out.println("The new Client is" + " " + newClient + " " + "with a discount of" + " " + discount + "%");
            }

                Menu menu = new Menu();
                menu.displayUserMenu();
            }
        }

Please suggest me how I am able to create new class that extends the parent class Client from the above user input. 


Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

You can achieve this by dynamically creating a new Java class file that extends the Client class bas... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!