Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between abstract and interfaces

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Saurabh V Vakil
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic