This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference Between Abstract Class and Interface

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the differences between Abstract Class and Interface are:
Abstract Classes:
1. can have constructors
2. cannot be instantiated
3. classes use 'extends' to extend abstract classes
Interfaces:
1. only have public final static data and public abstract
methods
2. all methods have void return type
3. interface methods cannot be final, static, synchronized,
or native
4. cannot have constructor
5. classes use 'implements' to implement interfaces
6. interfaces use 'extends' to extend other interfaces
Are there any other attributes can be added?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract Classes
----------------
1. Abstract classes can not have constructors as they can never be constructed.
Interfaces
----------
2. This is incorrect. methods can have any return type.
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Paul:
Abstract Classes
----------------
1. Abstract classes can not have constructors as they can never be constructed.

Thomas,
I think abstract class can have constructor(s), although it doesn't make sense. See the following code, it compiles and runs well and outputs 1.
abstract class Test {
static int i=1;
Test() {i=5;}
public static void main (String[] args) {
System.out.println(Test.i);
}
}
 
krutika burse
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ABSTRACT CLASSES :-
Can't have MULTIPLE INHERITANCE.
INTERFACES :-
Supports MULTIPLE INHERITANCE.
 
js yang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, both abstract classes and interfaces cannot be instantiated.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shows what happens when you respond before your first cup of coffee.
Abstract classes can have constructors which will be invoked when a concrete implementation of that abstract class is instantiated. Constructors themseves can not be abstract.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic