2

I am beginning to learn the principles of OOP and inheritance, and I came across this question while writing some code:

Suppose there is a package which contains a class called ClassA. Then, in a separate folder, I have another class called MyClass. Inside the same folder as MyClass, I have another class called ClassA, which is unrelated to the ClassA in the package. When I write the code for MyClass, I make it extend ClassA.

Which ClassA does MyClass extend from? Does MyClass inherit the ClassA which is in the imported package, or does MyClass inherit the ClassA which is in the same folder as MyClass? Would the code even compile?

I am trying to understand this from a theory perspective before diving into examples.

10
  • 2
    Look into import declarations and scope. Jul 6, 2017 at 19:16
  • 1
    Classes are identified by the unique combination package + class name. This enforces MyClassA can't appear twice in the same package and hence an import statement can only ever choose one. Jul 6, 2017 at 19:16
  • @LucianovanderVeekens There is no class called MyClassA. There is only MyClass and ClassA. What do you mean? Jul 6, 2017 at 19:17
  • 2
    It will extend whichever one you imported at the top of the java file. If you go up there and look at imports, it will specify one package or the other.
    – Araymer
    Jul 6, 2017 at 19:18
  • 1
    Your import will specify a package path, if it's in the same package, it will assume that one first. So, you would need to specifically import the one in a separate package.
    – Araymer
    Jul 6, 2017 at 19:24

4 Answers 4

1

what you're looking at is a Statically scoped language which will work its way out of its inner scope, all the way to its outter scopes. In this case, since import Class A is declared directly inside the file to which it is first called, it will use import Class A and stop.This will be its default behavior. It will not carry on to look at the packaged Class A because it found one already, declared inside of the same class file.

This is the default behavior of java's (static) scope hierarchy.

IF it had not found an import of Class A imported inside the same file, it would reach out to its package to search for one.

This is very useful when declaring like variables. Do a little research how statically scope languages work.

If it is easier for you to understand, you can be explicit in your intentions by declaring exactly which Class A you would like though.

Just a side note- this is more of a programming languages question than directly a java question, but since you ask specifically for java, we only need to cover the simple specific answer. if you would like to know more, i can direct you (or tell you) more about statically vs dynamically scoped languages.

I suppose it is worth noting that if you decide to import both Class As even from your package (which you do NOT need to do) you would have to explicitly declare which you would like.

1

In that situation, to make it perfectly clear to the compiler you would probably want to do something like extends otherPackage.ClassA, and use the full reference name to extend the classA from the other package. If you want to use the one from the package MyClass is in, then just don't import the other ClassA and do extends ClassA

0

Since you're new to programming, I'm going to explain it in really simple words. Say there is a package called Salads. In that package, you have a class called Caesar. Then, you have another package called People. In that package, you have another class called Caesar. Obviously, Salads.Caesar refers to Caesar salad, and People.Caesar refers to a person named Caesar. But both classes have the same name: Caesar.

So when you're writing java code, java looks in two places for class definitions:

  1. classes defined in the same folder (because they are implicitly in the same package if they are in the same folder assuming you're following all the normal rules.
  2. classes defined in any imported packages

So the question is asking if you just say Caesar in the code, will it recognize it as the one in the same folder or the one in the imported package? Well, this is a bad question to ask because first of all, you should not name your classes so ambiguously. Secondly, if it can't be helped, you should always refer to the fully qualified name in your code.

If you mean People.Caesar then type People.Caesar and if you mean Salads.Caesar, type Salads.Caesar. Don't take shortcuts. You can only take shortcuts if there is no ambiguity. The compiler will probably complain about it anyway asking you to specify. AKA your code will not work unless you change all references of Caesar to Salads.Caesar or People.Caesar.

0

Packages in Java is a mechanism to encapsulate a group of classes, interfaces and sub packages. Many implementations of Java use a hierarchical file system to manage source and class files. It is easy to organize class files into packages. All we need to do is put related class files in the same directory, give the directory a name that relates to the purpose of the classes, and add a line to the top of each class file that declares the package name, which is the same as the directory name where they reside.

in the top of java files, you have import that you can choose what class from what package you mean of course as @Jason said too if the class you want its in your package you don't need to tell it explicitly and compiler know that but if its in another package you have to tell him explicitly.

assume you have FirstClass.java in src folder and another in mycodes folder when in your class you import FirstClass you mean FirstClass.java that exist in src folder and when you import mycodes.FirstClass you mean FirstClass in mycodes folder.

your class can be member of packag.when you extend class that you class are in package A when you extend SomeClass you mean SomeClass that is in package A and if you want extend other class that is in other package like B you must extend B.SomClass

Here is another information about packages in java

7
  • i really think the OP meant by default which scope takes precedent rather than ways to explicitly declare which class
    – Jason V
    Jul 6, 2017 at 19:29
  • you are incorrect my friend. Look into statically scoped languages and youll find there IS an answer.
    – Jason V
    Jul 6, 2017 at 19:40
  • @Jason here we are talking about java Jul 6, 2017 at 19:43
  • and java is statically scoped. Therefore it will start at its inner scope and work its way out by default.
    – Jason V
    Jul 6, 2017 at 19:45
  • 1
    @Jason I fixed it :) Jul 6, 2017 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.