Hi all, I am reading a bit about inheritance, super classes and subclasses. Now when I build a java program I usually have a java file for the class and another one to test the class. I was wondering if I use a superclass and a subclass, should they be in the same file or in 2 different files?
thanks

It would depend on just what you are doing, but generally speaking, you would have them in separate files. Keep in mind that you can only have a single public class in a file.

Violet: you basically answered that question by each and every class you've written so far. all classes extend the Object class, but I pretty much doubt you'll write your classes in the Object.class code, right?

all that is needed for your class to extend a class, is that that class needs to be on the classpath of the application. if the application hasn't got access to the original class, it can't pass it's information on to a childclass.

but these classes don't need to be in the same package, they don't even have to be in the same package.
the only limits: your project needs to have the class you want to subclass in it's path, and: the class you want to extend may not be final. (String, for instance, is a final class, which means (among other things) you can not extend it)

Your folder structure needs to match the package structure, so what you just said is only true if the classes are all in the same package.

ok, not really familiar with "packages" (I presume you mean API?), is it something that you need to consider when using an IDE? I don't use any as yet and if I use more classes (I think I have done it only once) I have them in the same folder together with the test file

http://docs.oracle.com/javase/tutorial/java/concepts/package.html

Basically, you group related classes together into packages to help organise big systems, eg if your 99th application has a GUI, some business logic and a database interface you could group all its many classes into packages

application99.gui
application99.logic
application99.database

you source and class files the go into folders that match, eg

application99\gui\
application99\logic\
application99\database\

it's essential to get the correspondance between the package names and the directory structure, because that's how Java finds your files. Your IDE will alos organise its files the same way.

YOu will have already seen them in your import statements:
import javax.swing.JLabel;
JLabel is found in the swing package inside the javax package.

This article has been dead for over six months. Start a new discussion instead.