简体   繁体   中英

Polymorphism mimic for class in packages

OOP allows for inheritance for abstract methods, thus every child with own implementation. Such ability cascades to coding an application in general, as such, it calls to one same message at different times and receive different output according to the type of child instantiated. This concept is known as Polymorphism.

Picture it that Polymorphism gives a developer a chance to create an object. This object can be assigned a reference to one of multiple classes' instances that both contain a method of the same signature but of different implementation. This assignment is through conditional statements. Thereafter calling the method using this object, which in turn, happens to perform different operations depending on the appropriate condition.

With the same view but now we are dealing with a Java Application that has many packages that both contain a Class of the same name. A scenario all users of the Application go through a Login class then routes to a Main Menu screen (Main class). There are two different users thus two versions of the Main class, each of which are in two packages (be it: admin and user), in turn, acting as an entry point for the rest of the appropriate package.

Is there a Polymorphism mimic to let us declare an object in the Login class, of which during authentication, then assign a reference to either package mapping from the user type?

Thereafter, calling reference.Main.setVisible(true); will open the Main class for the type of the current login user.

Have both versions of the menu implement an interface/abstract class. Then use the interface polymorphically in the login class.

For example, make an abstract class called Menu, and have the userMenu and the adminMenu classes extend Menu. In your Login class, create a field of type Menu (for the sake of example call it m). Assign either a new UserMenu or AdminMenu at runtime, depending on who the user is. Then when you want to open the menu, call m.setVisible(true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM