Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use polymorphism and generics, as Java intended. </p> <p>Polymorphism lets you call a method on your object that behaves differently for every type. The easiest way to achieve this is to provide an abstract method in the base class <code>MasterClass</code> and then override it with different functionality in every extended class. You are probably looking for something like this:</p> <pre><code>class MasterClass { int age; // ... public abstract void doWork(); public int getAge() { return age; } // .. more methods } class User extends MasterClass { // customize User here @Override public void doWork() { /* User does work in some way */ } } class Customer extends MasterClass { // customize Customer here @Override public void doWork() { /* Customer does work in some other way */ } } // ... </code></pre> <p>If you are not too familiar with <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" rel="nofollow noreferrer">OOP</a>, <a href="http://docs.oracle.com/javase/tutorial/java/concepts/" rel="nofollow noreferrer">here</a> is a good introductory tutorial.</p> <p>If you are not allowed to alter your classes, you can populate a look-up table like <code>HashMap&lt;Class, MyFunctor&gt;</code> where you can assign a different <a href="https://stackoverflow.com/questions/7369460/help-with-understanding-a-function-object-or-functor-in-java">functor</a> for every type of person you have.</p> <p>Also, you might want to use <a href="http://en.wikipedia.org/wiki/Generics_in_Java" rel="nofollow noreferrer">generics</a>. Generics allow you to capture and restrict the type of objects passed to your methods.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload