• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

import vs extend?

 
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 211
Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right i'm already doing this and i will let you know about the result
 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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??









 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic