4
\$\begingroup\$

I'm trying to use inheritance and polymorphism to make it easy for the program to be enhance to many more rules. ClassificationRule is the base class and RuleFirstOccrnc and RuleOccrncCount and derived classes and by using the same non-static apply() method ClassificationRule should invoke all other non-static apply() method. I'm just starting on Java and I'm not sure if I'm doing it right. Can anyone tell me if I'm using inheritance or just calling the apply() method?

//PrioritRule Class
public class PrioritRuls {
 final static ArrayList<ClassificationRule> rulesA
 = new ArrayList<ClassificationRule>();
 static{
 rulesA.add( new RuleFirstOccrnc() );
 rulesA.add( new RuleOccrncCount() );
 }
 public static void prioritize( final String aUserInput ){
 for (ClassificationRule rule:rulesA)
 rule.apply(aUserInput);
 }
}
// ClassificationRule Class - Based Class
public class ClassificationRule {
 public void apply (final String aUserInput) {
 apply( aUserInput );
 }
}
//RuleFirstOccrnc - Derived Class
public class RuleFirstOccrnc extends ClassificationRule {
 public void apply ( final String aUserInput ) {
 for( TweetCat t: TwtClassif.tCat )
 applyFirstOccurrenceRuleTo( t, aUserInput );
}
 * * *
//RuleOccrncCount - Derived Class
public class RuleOccrncCount extends ClassificationRule {
 public void apply ( final String aUserInput ) {
 for( TweetCat t: TwtClassif.tCat )
 applyOccurrenceCountRuleTo( t, aUserInput );
}
 * * *
palacsint
30.3k9 gold badges82 silver badges157 bronze badges
asked Apr 10, 2012 at 0:35
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Do yourself (and everybody else) a favor - don't use arbitrarily shortened class (or variable) names. Also, what's the point of using prefixed or suffixed variable names (aUserInput)? It makes it harder to tell what you're doing, without giving you any benefit. \$\endgroup\$ Commented Apr 30, 2012 at 16:28

1 Answer 1

3
\$\begingroup\$

In your code, two classes differently extend a base class ClassificationRule. Objects of these classes are added to a list, cast as the base class. They are retrieved from the list and a method, apply(), which is present in the base class but overridden in the child classes. As you intend, the apply() method as present in the child class will run, not the apply() method from the base class.

In the case of your code, the child class does not actually gain functionality from the base class. You are inheriting solely in order manipulate objects of different types as if they were of one homogenous type, because you know they provide certain functionality.

In software engineering terms, you would say 'the classes implement an interface'. An interface looks very much like an empty base class, like the one you wrote, and it is a Java language feature different from a base class. Implementing an interface (i.e. providing some agreed functionality) is different from inheriting functionality.

And the code for it is different, you would write public interface ClassificationRule {, and public class RuleFirstOccrnc implements ClassificationRule {.

In Java, you can only inherit from one class (thought that class may itself have a parent). But you can implement as many interfaces as you like. A typical java object will implement interfaces that let it be sorted (comparable), serialized to transmissable data (serializable), etc, but can only ever inherit from a single base class. C++ lets you inherit multiple base classes. Things go horribly wrong when you do. Or at least when I do.

palacsint
30.3k9 gold badges82 silver badges157 bronze badges
answered Apr 10, 2012 at 1:04
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.