The Artima Developer Community
Sponsored Link

Java Answers Forum
difference between class and interface

6 replies on 1 page. Most recent reply: Jun 25, 2011 5:00 AM by Bret Brown

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
ram maradolla

Posts: 3
Nickname: maradola
Registered: Mar, 2007

difference between class and interface Posted: Mar 27, 2007 4:05 PM
Reply to this message Reply
Advertisement
w't is the difference between class and interface


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: difference between class and interface Posted: Mar 28, 2007 12:52 AM
Reply to this message Reply
A interface defines, which method a class has to implement.
This way - if you want to call a method defined by an interface - you don't need to know the exact class type of an object, you only need to know that it implements a specific interface.


Example:

    interface Printer {
        public void print(String text);
    }
 
    class FilePrinter implements Printer {
        public void print(String text) {
           //append the text to a file
        }
    }
 
    class ScreenPrinter implements Printer {
        public void print(String text) {
           //write the text on the screen
        }
    }
 
    class SomeClass {
        public printSomething(Printer myPrinter) {
            myPrinter.print("Hello");
        }
    }


If you call SomeClass.printSomething(...) it does not matter if you pass an instance of FilePrinter or ScreenPrinter, because the method just does not care. It knows that the object implements the interface Printer and also implements it's methods.

Another important point about interfaces is that a class can implement multiple interfaces.

guru prasad

Posts: 1
Nickname: agrgp
Registered: Mar, 2007

Re: difference between class and interface Posted: Mar 28, 2007 2:09 AM
Reply to this message Reply
interface is little bit like a class... but interface is lack in instance variables....that's u can't create object for it.....
2.interfaces r developed to support multiple inheritance...

3.the methods present in interfaces r pure abstract..

4.the access specifiers public,private,protected r possible with classes.but the interface uses only one spcifier public.....

5.interfaces contains only the method declarations.... no definitions.......

pinjala sowmya

Posts: 1
Nickname: psoumya
Registered: Nov, 2008

Re: difference between class and interface Posted: Nov 24, 2008 4:53 AM
Reply to this message Reply
Interfaces are syntactically similar to classes, but they lack instance variables,and their methods are declared without any body.One class can implement any number of interfaces.Interfaces are designed to support dynamic method resolution at run time.

kaustubh tawade

Posts: 1
Nickname: kauty
Registered: Mar, 2011

Re: difference between class and interface Posted: Mar 12, 2011 9:18 PM
Reply to this message Reply
hii i have a doubt in my code

import javax.media;public class VideoApplet extends JApplet implements ControllerLsitener {Player player=null;
public void init(){URL mediaurl=new URL(getCodeBase()+"1.mpg")
player=Manager.createPlayer(mediaurl);} 


here Player is a interface ,so how an instance is create Player player=null

Dave Lorde

Posts: 8
Nickname: dlorde
Registered: Aug, 2005

Re: difference between class and interface Posted: Mar 27, 2011 9:58 AM
Reply to this message Reply
The call to Manager.createPlayer(..) creates and returns an instance of some class that implements the Player interface. The particular class used probably depends on the argument passed to createPlayer(..). This is generally known as a 'factory method' because it uses its arguments to create objects of a certain base type (Player in this case).

The calling class doesn't need to know anything about the object returned except that it is a Player, i.e. it implements the Player interface.

Bret Brown

Posts: 5
Nickname: bretwt91
Registered: Jun, 2011

Re: difference between class and interface Posted: Jun 25, 2011 5:00 AM
Reply to this message Reply
Following are the difference between the Classes and Interface

1. The interfaces are used in java to implementing
the concept of multiple inheritence whereas classes
are not used to implement multiple inheriyence.

2. We are not allocating the memory for the
interfaces whereas the memory is allocated for
the classes

3. Interfaces are always implemented whereas clases
are always extended

Flat View: This topic has 6 replies on 1 page
Topic: OutOfMemoryError: Java heap space Previous Topic   Next Topic Topic: Java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use