File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes import vs extend? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "import vs extend?" Watch "import vs extend?" New topic
Author

import vs extend?

Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Hi folks,,

what is the difference btw importing and extending a class interms of Modifiers?

as we know we have two access modifiers for classes
1-public access
2-Default access : without the public keyword

if we have two classess in two different classes



please consider the above scenario with all modifers

final,,abstract, public & default thanks


``Worry does not empty tomorrow of its sorrow; it empties today of its strength.''
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hello Hama Kamal,
I have a 'pop-quiz' that might even lead to figuring out the answers to your doubts... it is as follows;

-What is the use of import statements?
-Why do we extend classes?

Let me know what you think, and if this does not clarify your doubts we can continue from there...

Regards Ikpefua.


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 202


HI,
You are having some misconception, i will make you understand.


Classes with default access is available or accessible only to classes within same package where as in the case of public it is accessible to all the classes in java Universe.

example.
-----------
package p;
class A
{
}
Now class A is having default so it can only be accessible to classes in package P

So in order to use class A or if you wanna make a reference to it in your class then you need to either use its fully qualified name or
you simply import it.
by importing you give a short name to the fully qualified name

import java.p.A;
class Your_Class
{
A obj=new A();
}

if you had not imported then you would have to use fully qualified name i.e

class Your_Class
{
p.A obj=new p.A();
}
Both the examples are legal providing they are in same package p
In case of public modifier to a class it is available to whole Java Universe.

I hope i helped You..


OCJP-90%,OCPWCD-95%
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hama Kamal wrote:what is the difference btw importing and extending a class interms of Modifiers?
but can we import it without extending ?

The first question is somewhat 'ambiguous', because a specific scenario will make one explain better, now the second question(quite specific) has a direct answer 'YES'. This is because an import statement and inheritance(extends) are two different things, However it is important to understand both concepts.
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Ikpefua Jacob-Obinyan wrote:Hello Hama Kamal,
I have a 'pop-quiz' that might even lead to figuring out the answers to your doubts... it is as follows;

-What is the use of import statements?
-Why do we extend classes?

Let me know what you think, and if this does not clarify your doubts we can continue from there...

Regards Ikpefua.


Hello Ikpefua

find my answers below
-What is the use of import statements?

we use import statements so that we can import classess from different packes and make objects of them in our class and later to access it's methods or instance variables

-Why do we extend classes?

we ecxten classes to get features of the super classes

so what i understand is extending is inheritance but importing is not ,, am i right?
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

sagar shroff wrote:
HI,
You are having some misconception, i will make you understand.


Classes with default access is available or accessible only to classes within same package where as in the case of public it is accessible to all the classes in java Universe.

example.
-----------
package p;
class A
{
}
Now class A is having default so it can only be accessible to classes in package P

So in order to use class A or if you wanna make a reference to it in your class then you need to either use its fully qualified name or
you simply import it.
by importing you give a short name to the fully qualified name

import java.p.A;
class Your_Class
{
A obj=new A();
}

if you had not imported then you would have to use fully qualified name i.e

class Your_Class
{
p.A obj=new p.A();
}
Both the examples are legal providing they are in same package p
In case of public modifier to a class it is available to whole Java Universe.

I hope i helped You..



well Sagar ,, that is really helpful,,,, i have two more questiona

1- we can not extend a class marked with final ,,, am i right?
2-but we can import a class which is marked as final ,,,,,,,,, right?
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 202

Yeah you cannot extend final classes but you are always allowed to make a reference of final classes so therefore you can always import it.
Always take care while dealing with such questions,you should first ask yourself whether the class itself is accessible and then you can import or extend anything that you like.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hama wrote:
we use import statements so that we can import classess from different packes and make objects of them in our class and later to access it's methods or instance variables

Hello Hama, import statements -to the best of my knowledge- are to make codes easier to read, lets take a look at some codes that explains this better.


Wthout import

With import

Did you notice that without import you MUST use the fully qualified name of the class Feline, even during instantiation?

I hope you understand it better now.

Hama wrote:so what i understand is extending is inheritance but importing is not ,, am i right?


Yes your understanding is correct.

Regards

Ikpefua.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206

Below is a modified version of Hama Kamal code. Have a review, answer each question and see if you answer them correctly and understand why.

Next try to test and run the code and see how they work. If they're not working, what change do you make to make them work? Will follow up with explanation in details
later if you're not clear.

Just a little challenge for a review...this type of scenario required knowledge of inheritance, access modifiers, package etc.




OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Tommy Delson wrote:
Below is a modified version of Hama Kamal code. Have a review, answer each question and see if you answer them correctly and understand why.

Next try to test and run the code and see how they work. If they're not working, what change do you make to make them work? Will follow up with explanation in details
later if you're not clear.

Just a little challenge for a review...this type of scenario required knowledge of inheritance, access modifiers, package etc.




Hi Tommy ,, thanks for your test, and check my answer a bove
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Hello guys,, many thanks for your explanations ,,,,,,


please check if what i concluded is correct or not : i have tried them practically please correct me if you think i am not correct

1-you can import or extend a public class from a class in a differetn package
2-you can't import or extend a non public class from a class in a differetn package
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hello Hama,
can you try everything you have just asked by coding?...Yes I mean write test programs, create packages, declare classes inside the packages, carryout imports and extends of classes between packages with different access modifiers and write down your findings, "personal experience is the best teacher".

I hope this helps.

Regards

Ikpefua.
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

you are right i'm already doing this and i will let you know about the result
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Ikpefua Jacob-Obinyan wrote:
Hama Kamal wrote:what is the difference btw importing and extending a class interms of Modifiers?
but can we import it without extending ?

The first question is somewhat 'ambiguous', because a specific scenario will make one explain better, now the second question(quite specific) has a direct answer 'YES'. This is because an import statement and inheritance(extends) are two different things, However it is important to understand both concepts.


hey Ikpefua

as i tried i found that we can not do that also

now the second question(quite specific) has a direct answer 'YES'. This is because an import statement and inheritance(extends) are two different things, However it is important to understand both concepts.

so No we can not do that ,,,,,,,, please try and see
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Ikpefua Jacob-Obinyan wrote:Hello Hama,
can you try everything you have just asked by coding?...Yes I mean write test programs, create packages, declare classes inside the packages, carryout imports and extends of classes between packages with different access modifiers and write down your findings, "personal experience is the best teacher".

I hope this helps.

Regards

Ikpefua.


i did that ikpefua and corrected my post whihc is just above your post ,, please check it
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206

Hi Tommy ,, thanks for your test, and check my answer a bove
this class can not run because class tea can't extend class Drink Since they are in different packages and class Drink is not public and hence all of the below fails!

@Hama
Good, you got partial right, explanation in analysis details is more helpful. See below for details analysis...

Brief Analysis:

1. Drink class can extends class Beverage and inherited all member of Beverage since Drink in the same package "cert". Hence, any class in the same package can access to Beverage's class members with a default access modifier.

2. Tea can extends Drink, but cannot inherited all member of Tea since Tea class is in different package "exam.stuff" and Drink class has default access modifier. Hence, any class from a subclass outside the package cannot access to Tea's class members.

See K & B book on page 51 for more insight.


OK, what if we change access modifier of Beverage to public and Drink to protected and everything else is the same ?? What are the answer and why??









 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: import vs extend?
 
Similar Threads
packages
Certification cx 310-035
what happens at runtime?
Question on Packages from Kathy Sierra Book
K&B: :Page 14 Default Access - misleading sentence