• We Code
  • We Design
  • We Develope
  • We Write
  • We Share


Wednesday, September 12, 2012

Abstract class In Java FAQ

FAQ about interface And Abstract Class in Java 

Why interface variables are final?

To identify variable  ambiguity at compile time and to avoid it at runtime .

Why interface variables are static also?

There are two reasons why interface variables are static also

  •  First reason is because we cannot instantiate interface without instantiating interface,interface vars can be accessed by directly using interface name.
  •  second reason is interface variables are final hence it must be initialized at the time of declaration only.
When the same interface is derived into subclass and if we create multiple instances of a class,for each instance of subclass one copy of memory is allocated final variable.That means if sub class is instantiated for 10 times ,10 times memory is alocatedto final variable.Since its value can not be changed why unnecessary copy of memory allocation is required for final variable instead of having only copy of memory.Variable to which we want one copy of memory such variables are declared as static .That's why most of final variable are static too.

Why interface method are abstract ?

Java does support multiple inheritance (we know this fact). Java class inherit not only super class and can implement from any no of inheritance from one super class methods are derived with implementations and from many interfaces methods names and signatures are only derived but not methods with implementation class which implement interface must provide implementation to all of its methods .

Is it true ? 
yes this is how Java supports multiple inheritance partially (means methods name and signature are derived but not methods with implementation). This happened because abstract methods only . 
Then the immediate question would be why we need to derive from the interface ,inherit abstract method and implement them in sub class ?

Why can't we directly implement them in a class ?

To maintain contract between two different parties means contract between vendor-vendor ,developer-vendor,vendor-developer.


Post a Comment