This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

extending a class and importing packages  RSS feed

 
Charles Knell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a class that did very nearly the same thing as I needed in a new situation, so I decided to created a new class that extended the original and over-ride the method that I needed to behave differently.

The original class begins this way:

package com.kilonovember.sars;
import java.io.*;
import java.util.regex.*;
import java.util.StringTokenizer;

So I opened my trusty editor and began my new class like this:

package com.kilonovember.sarsj;

public class TemplatePrep extends com.kilonovember.sars.PostProcessRegexSwap {

and I pasted a copy of the method I wanted to over-ride in the middle.

When I tried to compile the new class, error messages abounded. Looking at them, I determined that the way to get the class to compile was to repeat the imports from the original class in my new class.

That worked, but after leafing through the books at my disposal and Googling, I can't find any example code that does the same thing. I suppose, for simplicity's sake, that details like that are left out of examples, but it's frustrating to have spent hundereds on books and still be forced to learn what seems to be such a basic requirement by trial-and-error.

Are there any other things that "everybody knows" that are commonly left out of example code and instructions that have caused you trouble?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I tried to compile the new class, error messages abounded. Looking at them, I determined that the way to get the class to compile was to repeat the imports from the original class in my new class.


As with any class, you need to import the classes that you use. The reason that you need to "repeat" is because your new class seems to need everything the original did.

Henry
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to repeat the imports from the superclass definition in the subclass source code. You do have to import whatever classes you need into the source file where you define the subclass. It sounds like you were assuming that the subclass would in some way "inherit" the imports from the superclass, but, although that's a sensible theory, that's just not how imports work. Imports are just instructions to the compiler; they have absolutely no lasting effects on the generated code. All they do is tell the compiler, for example, "When I say InputStream, I mean java.io.InputStream." The imports are not recorded in any way into a class file; therefore, of course, they can't be inherited by subclasses in the way you imagine.

Now, as to your last question: many books leave out imports, and also many books leave out class bodies, and sometimes even method declarations. For example, a book might say

System.out.println("foo");

when of course, you can't do anything with this one line of code; you have to turn it into



I don't think there are any major gotchas here; once you've learned that you have to write a complete class, and that statements have to go inside methods, it's generally not a problem.
 
Charles Knell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.

Now I've got the new class written an compiled and jar'ed. When I execute the new class the method I had hoped to override from the base class is being executed and not the method of the same name that I placed into the derived class.

What am I missing?

Thanks in advance.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the method arguments the same? If they differ even a little, then you may be overloading the method rather than overriding it.

Is whatever is invoking the method operating on an instance of your new class, rather than the old one? In other words, is something calling "new TemplatePrep()" rather than "new PostProcessRegexSwap()"?
 
Charles Knell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither the original method, nor the method in the subclass take any arguments.

My subclass looks like this:

package com.kilonovember.sars;
import java.io.*;
import java.util.regex.*;
import java.util.StringTokenizer;


public class TemplatePrep extends com.kilonovember.sars.PostProcessRegexSwap {
public TemplatePrep(){
super();
}
public static void swapRegex(){
/* New code goes here. It isn't executed.*/
}
}

After reading your most recent post, I tried this modification, but with no different result.

package com.kilonovember.sars;
import java.io.*;
import java.util.regex.*;
import java.util.StringTokenizer;


public class TemplatePrep extends com.kilonovember.sars.PostProcessRegexSwap {
public TemplatePrep(){
TemplatePrep tp = new TemplatePrep();
/* The first and third methods are from the base class.
Only the middle one is over-ridden.
*/
tp.loadFile();
tp.swapRegex();
tp.saveFile();
}
public static void swapRegex(){
/* New code goes here. It isn't executed.*/
}
}

What am I missing?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Charles Knell:

What am I missing?


Just writing a subclass of a class isn't enough. The code that currently does something using the parent class must be changed -- or new code written -- to use the new class instead. Just defining a subclass of PostProcessRegexSwap isn't going to magically replace all uses of PostProcessRegexSwap with uses of TemplatePrep. If this is an application, for example, there's a "main()" method somewhere that's (directly or indirectly) currently creating a PostProcessRegexSwap . It will continue to do so, blithely ignorant of the existence of TemplatePrep, until you change it. If it's not an application, then it might be a Servlet's "doPost()" or an Applet's "init()" you need to change.

Make sense?
 
Charles Knell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to execute the derived class on the command line.

The main is in the base class.

So, you are saying that I need to put a main{} method in my derived class and mimic the code from the base class?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably. There are ways to make relatively small changes to the base class that would then allow you to define as many subclasses as you want without ever needing to redefine main(), but the explanation could get pretty involved. I think a simple copy-paste-modify, and learning from the experience, would be the best thing to do right now. Once that's working for you, c'mon back and ask about more complex solutions if you're interested.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!