wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between abstract and interfaces 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 "Difference between abstract and interfaces" Watch "Difference between abstract and interfaces" New topic
Author

Difference between abstract and interfaces

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Hello Ranchers,

Need your help.
I am bit confused between abstract and interface.
Abstract class defines and implements methods and cannot be instantiated.
Interface only dfines methods but can't implement those methods itself.
What this means??

For eg:

Abstract class

public abstract class A
{
public abstract void set();
public abstract String get();
}

public class B extends A
{
public abstract void set()
{
Strin name ="Agra"
}

public abstract String get()
{
return name();
}

public class demo
{
public static void main(String []args)
{

B b1= new B();
b.set();
String d = b.get();
System.out.print("d);
}
}

//Interface

interface A
{
public void set();
public String get();
}

class demo implements A
{
public void set()
{
String f ="Juhu"
}
public string get()
{
return f();
}

public class test
{
public static void main(String []a)
{
demo d1 =new d1();
d1.set();
String h = d1.get();
System.out.println(h);
}
}

My question is that in above examples we are defining and also implementing then what is difference between two abstract and interface?

Please help me out...

Thanks in advance



Thanks<br />Dinesh
Saurabh V Vakil
Greenhorn

Joined: Jan 30, 2008
Posts: 14
All methods in an interface need to be abstract methods whereas in an abstract class, there must be at least one abstract method and there can be one or more non-abstract methods.
vatsalya rao
Ranch Hand

Joined: Feb 14, 2007
Posts: 63
Hello Dinesh,

Did you compile your code first of all?

apart from errors like ";" expected and "unclosed string literal" you will get 6/7 errors.

Iam giving you clues:

1)only one public class per file.
2)If you are extending an abstarct class but not overriding its methods,then your class must be declared abstract.

3)abstract methods can not have a body.


you compile your code of absstract classes first,that will surely give you some clarification about abstract classes.




Abstract classes can have method implementations in it,where as interfaces can not.


you can not instantiate an interface,but where as when you instantiate a concrete subclass that extends an abstract class,the abstract class constructor is called.

When an instance of B is created,its parent class constructor is called though its parent is an abtract class.

abstract class A
{

A()
{
System.out.println("A");
}

public abstract void display();//abstract methods should not have body

}

class B extends A

{

B()
{

System.out.println("B");

}

public void display()
{

System.out.println("display in concrete class");

}

}

public class demo
{
public static void main(String []args)
{

B b1=new B();
b1.display();

}

}


An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.

An abstract class can contain fields, constructors,An interface can not contain fields, constructors, it has only the method signature but no implementation.

An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between abstract and interfaces
 
Similar Threads
problem i faced in cloning
an abstract class implementing an interface
Anonymous Inner Class from K&B
Enum
some doubts