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
Step: 1
You can achieve this by dynamically creating a new Java class file that extends the Client class bas...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started