aspose file tools*
The moose likes Java in General and the fly likes can we create object of the interface and what is the base package of java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "can we create object of the interface and what is the base package of java ?" Watch "can we create object of the interface and what is the base package of java ?" New topic
Author

can we create object of the interface and what is the base package of java ?

shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 67

1. can we create object of the interface ?
2. what is the base package of java ?

with regards,
Shivdhway Pandey
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

1. No, you cannot instantiate an interface. An interface only declares methods but does not contain any implementation of those methods. If you could instantiate an interface, then what would happen if you'd try to call a method on the object?

2. There is no common base package.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 67

then what is the java.lang or java.io or java.util in these java is what a package or something else as per me lang is default to use then either java should be a base package or java.lang
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
The default is use neither. The default is to use the folder you are in as the unnamed package. Classes in java.lang are used frequently, so need not be imported, but that doesn’t mean java.lang is some sort of “base”.

You can of course create anonymous classes which implement an interface.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Is there actually a package called java? I don’t remember ever seeing it.
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 67

what about the package not class in java default package is there any in java
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

shivdhwaj pandey wrote:what about the package not class in java default package is there any in java

Erm, you'll have to rephrase that.

However, rather than worrying about what you think it should be, I think you'd be better off learning how it is (at least at this stage). You'll find it less stressful.

Plenty of time for e-mails to the designers of Java later on.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

If you do not put a package somename; statement at the top of your source code file, then your class will be in the default, unnamed package. Not in java.lang or any other package. It's not recommended to put classes in the default package.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
.... and classes in default packages cannot be imported and used in other classes in named packages..
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 67

classes those are default can be used as there name says that they belo9ngs to default package
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

shivdhwaj pandey wrote:classes those are default can be used as there name says that they belo9ngs to default package


The "default" package is no package at all. You should only ever use that for tiny one-time, standalone classes you use for simple testing.

You can refer to another class without using its package name under the following circumstances:

1) It is in the same package as your current class.

2) It has been imported.

And, for #2, all the classes in the java.lang package are implicitly imported. That is, it's as if there were an import java.lang.*; statement at the top of every .java file, so you never need to explicitly import those classes to use them without their fully qualified name.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

shivdhwaj pandey wrote:classes those are default can be used as there name says that they belo9ngs to default package

Yes, they can be used, but they cannot be imported, as John Jai says.

Suppose that you have a class "SomeClass" in the default package (so, there is no "package" statement in SomeClass.java). Suppose that you also have a class "AnotherClass" in a package "org.example.mypackage". Now there is no way to import SomeClass inside AnotherClass.java. If you would try this, you would get an error:

(Note that this used to be possible in old versions of Java. It is, as far as I know, the only feature that has ever been removed in a newer version of Java).
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

you certainly can instantiate an interface!
consider AudioClip which is an interface and parent which is a reference to the applet. the following code works great.

private AudioClip animal;

public void mousePressed(MouseEvent e)
{
animal = parent.getAudioClip(parent.getDocumentBase(),parent.animals[parent.selection].getAudio());
animal.play();
}


SCJP
Visit my download page
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Randall Twede wrote:you certainly can instantiate an interface!


No. (Unless you're playing at semantics, but if so, please don't as it will just confuse beginners.)

You can only instantiate concrete classes. Now, sure, if one of those classes implements AudioClip, then, yes, it "IS-A" AudioClip, so you are instantiating something that is an AudioClip. You cannot simply instantiate a bare interface though.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

well yeah, the applet class must implement AudioClip. the code however does indeed instantiate an interface by assigning it a concrete class. you can call that semantics if you wish. you are right though, it is confusing. it confused me when i first realized AudioClip was an interface.
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 67

thanks to all of you
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Randall Twede wrote:well yeah, the applet class must implement AudioClip. the code however does indeed instantiate an interface by assigning it a concrete class. you can call that semantics if you wish. you are right though, it is confusing. it confused me when i first realized AudioClip was an interface.

I think what Jeff means here is that if AudioClip is an interface, then you can not do this:

Instantiating a concrete class implementing this interface and assigning the newly created object to a reference of AudioClip can not be called instantiating the interface itself.

This is pretty basic, but laying down fundamentals like these in clear words definitely helps new programmers.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Randall Twede wrote:well yeah, the applet class must implement AudioClip. the code however does indeed instantiate an interface by assigning it a concrete class.


You may find this quibbling, but if by "assigning it a concrete class", you mean "assigning the AudoClip variable a reference to an instance of a concrete class," then no, that's definitely not "instantiating an AudioClip." Assigning a value to a variable is simply that--an assignment. It's absolutely not instantiation, not even close. Instantiation means to create an instance of an object. Instantiation and assignment are completely separate, independent, an unrelated actions.

Sorry if I'm being nitpicky, but I feel it's important to make these distinctions clear, especially for beginners who are still trying to get their heads around the concepts these terms refer to.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i stand corrected
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 67

true is always true ,no matter who says
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can we create object of the interface and what is the base package of java ?