Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Difference Between Abstract Class and Interface

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ABSTRACT CLASSES :-
Can't have MULTIPLE INHERITANCE.
INTERFACES :-
Supports MULTIPLE INHERITANCE.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, both abstract classes and interfaces cannot be instantiated.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic