• 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

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

 
Ranch Hand
Posts: 67
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. can we create object of the interface ?
2. what is the base package of java ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
shivdhwaj pandey
Ranch Hand
Posts: 67
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there actually a package called java? I don’t remember ever seeing it.
 
shivdhwaj pandey
Ranch Hand
Posts: 67
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what about the package not class in java default package is there any in java
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.... and classes in default packages cannot be imported and used in other classes in named packages..
 
shivdhwaj pandey
Ranch Hand
Posts: 67
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
classes those are default can be used as there name says that they belo9ngs to default package
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4716
9
Scala Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to all of you
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i stand corrected
 
shivdhwaj pandey
Ranch Hand
Posts: 67
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
true is always true ,no matter who says
reply
    Bookmark Topic Watch Topic
  • New Topic