-1

I know that we can't create object for interface and abstract class. my doubt is why? why can't we create an object for interface and abstract class? for example

class Demo{

 }
Demo demo = new Demo();//it is possible

but

interface Demo{

}

Demo demo = new Demo();// it is not possible why?
  • I think interface defines the structure of a class, so it is not a class by itself. – Soley Nov 01 '15 at 13:21
  • I heard that interface internally acts as super class. and in according to my knowledge in interface we can declare a method, but we can't define it. @Salivan – subramanyemm Nov 01 '15 at 13:24

2 Answers2

1

An object in java contains data (class members) and set of operations (methods). These operations are performed on the data stored by the object.

Now both interface and abstract class don't provide concrete implementation of operations, which could be performed on the object data. Basically interface and abstract classes in the java are used to define/declare the contract for an object. This contract of operations is later fulfilled by the concrete/complete classes.

So these incomplete objects will be of no use in the application. So java does not allow creating instance of interface or abstract classes.

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
0

Because both interface and abstract class could have abstract methods which doesn't have implementation that's why is unclear what new Demo().myMethod() would do.

Adrian
  • 693
  • 5
  • 19