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

menu

Wednesday, September 12, 2012

Abstract class In Java FAQ

3 comments

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.
Read more ►

Tuesday, September 11, 2012

type of classes -interface

0 comments


Interface

Interface in java programming language  is equivalent to class , but it is a pure abstract class.

There are many benefits of interface in java programming language but the current topic doesn't  fit to explain about all practical benifits of interfce. Hence we can briefly discuss about interface here.


  1. All methods in the interface are by default declared as abstract.
  2. We can declare only method with parameters and return type alone in interface
  3. we cannot write any method with implementation in interface.
  4. when method do't have body such methods are declared as abstract.
  5. While declaring interface,in place of class keyword we must use interface keyword.



For Examples :
  public interface Myinterface
              {
               
               }

Interface variables are by default declared as static and final.The reason is to avoid/prevent runtime variable ambiguity problem.


It can be described as before other OOP languages has support for multiple inheritance . In multiple inheritance in the same variable derives from more than one super class,suppose in future if we refer super.var in sub class,at runtime the runtime environment detect and asks that the variable came from two super classes from which super class the variables want to be taken into consideration . It is a ambiguous problem.


But whereas in java the variables of interfaces are final and static by default .Suppose class B derives  from super class A,and also derives from interfce C and D. In the  same variable derives from both super class and from one super interface,the recent super interface variables overrider class variable and also becomes final. If class B derives from another interface also that contains the same variable compiler detect ambiguous error at compilation time itself . Hence variable ambiguity is solved at the time of compilation .
Variables which are declared as final are not modifiable but if they are not declared as static ,for every instance of interface (means its sub class) new memory allocates to variable that leads to duplication of memory consumption.

To avoid unnecessary memory consumption interface variables are declared as static so that to any number instance of a sub class only one copy of memory is allocated static variables.
that's why interface variables are static and final.





//Ainterface.java
public interface Ainterface
  {
   
   public static final int i=10;
  } 



//Binterface.java

public interface Binterface
  {
   
   public static final int i=10;
  } 
Read more ►

Monday, September 3, 2012

oops Concept In JAVA

2 comments
The oop is mainly based on 4 concept

  1. Encapsulation
  2. Abstraction
  3. Inheritance
  4. Polymorphism 
Encapsulation :  Encapsulation encapsulates both data and function . Its nothing but the binding of variables and methods


Abstraction : Abstraction does not only mean declaring methods/functions and class as abstract.

its main aim is to hide the implementation details of method and exposes only functionality.


Inheritance :The two main aim of OOPS is to achieve code re-usability and runtime binding of code.



code re-usability  we can achieve one with inheritance and the other one is through instantiation (creating object of  the class).Inheritance mean writing a class with required variables and functions that are common to more than sub class and deriving/extending that class into many sub classes is called as inheritance.

All the variables and functions of the super class become the variables and functions of sub class except private members(variables/functions).
The best example for inheritance is deriving label,TextField and Button classes from component super class.
Polymorphism : We can classify polymorphism into 2 types:

  •       Overloading
  •       Overriding
   Overloading :overloading in java means writing same function for many times with different function signatures (number of parameters and types of parameters) but not the return type of the function.Functions may belong to supper class or sub classes.
    Overriding :Overriding in java means rewriting super class function in sub class with the same signature and return type is called as function overriding.    
Compile time binding means  static binding and Runtime binding  means Dynamic binding
Read more ►