Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface is nothing but a pure abstract class. why interface does not has constructor

 
ranjithkumar.gendhe kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface is nothing but a pure abstract class. why interface does not has constructor
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Becasue constructors are used to initialize instances and interfaces cannot be instantiated.
 
ranjithkumar.gendhe kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but abstract class has constructor. why
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An abstract class is not an interface. It is a base class that can be extended by another class. As such it can be instantiated indirectly. Interfaces cannot.

Saying that "interface is nothing but a pure abstract class" is not correct. Though they may seem similar, they are not the same. A class can extend only one abstract base class, but can implement any number of interfaces.
 
ranjithkumar.gendhe kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you just clarify this one


i created a java file. it contains only interface. when i compile this one it will create a class file.class file created only for class is it true?

 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class file created only for class is it true?

Nope. Enums are also compiled to class files.
 
ranjithkumar.gendhe kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya you are right it has constructor. but interface does not has constructor.

i created a file contains interface when compile this one compiler creates .class file to interface. what happening internally
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to see a connection between having a constructor and being a class file; that connection is spurious at best - better not to get attached to it.

I think the Java Virtual Machine specification, particularly chapter 4 "The class File Format" can help you. You may also wish to consult the Java Language Specification.
 
Kiaamaa Liammes
Ranch Hand
Posts: 32
Eclipse IDE Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when a class is compiled , it is converted into executable byte code , which is the .class file, on the other hand constructors are used to create instances i.e objects of a class
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic