• 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 ►

Sunday, August 26, 2012

switch and for statement

0 comments
This post of java learning course is focused on java switch statement  and java for statement.
The java course  syntex for java  switch statement and java for  statement is listed below.

Switch Statement

Switch statement helps us to select/choose out of many choices.

switch (expression)
 {
  
   case var1:statement1;
            :break;
   case var2:statement2;
            :break;
   case var3:statement3;
            :break;
   case var4:statement4;
            :break;
   case var5:statement5;
            :break;
   case var6:statement6;
            :break;
   case var7:statement7;
            :break;
   case var8:statement8;
            :break;
   default:  statement9;
}


For loop

This is a self contained looping statement that contain initialisation/initial value,termination condition & increment/decrement

for(var declaration-cum initialization;condition;increment/decrement)
  {
   some codes
  }

Read more ►

Branching in JAVA or Control statements in JAVA

0 comments

Control statements are used to make branching (if,if-else,if-else-if etc) ,looping(for,while,do-while),skiping(switch) and exiting block of code(countinue,break)



  1. if counditions
  2. if()
  3. if(),else
  4. if(),else if(),else



if()  tells whether the condition is satisfied or not ,if condition satisfies the following block of if() code (also called as if() condition body )  will execute.



if(coundition)

 {
   do something
  }

if(),else


this statement helps to select one possible block of code out of two conditions.


if(coundition)

 {
   do something
  }
else
{
  do something else
}

if(),else if(),else

                 This statement helps to select one possible block of code out of three conditions.


if(condition)

 {
   do something
  }
else  if(another condition)
 {
   do something else
  }
else
{
  do something else
}

Nasted if conditions


In this we will write conditon under an another condition .


if(condition 1)

{
    if(coundition 2)
      {
         
        if(condition 3)
          {
            do something
           }
          
        else (condition 4)   
           {
             do something
            }
     
     else (condition 5)
      {          
         do something
       }

else(condition 6)

  {
    do something
 }
      

Read more ►

Thursday, August 23, 2012

Relational Operators in JAVA

2 comments
Relational operators in JAVA are the binary operator, as we know know binary operator take two parameter .For comparing values of variables ad literals Java provided relational operators. Relational operators relate a given value with several possible values of a variable . The result of the relational operators help to make branching iterating a block and to terminate the block statement

Relational operator returns boolean i.e true or false





==            equal to

!=             not equal to
>               greater than
>=            greater than equal to
<              less  than
<=            less than equal to




for instance/example




int a = 25;

int b=75;
double x=35.87;
double x=85.87;
char c1='b';
char c2='c';
..............................................
a=b    false
a!=b   true
a<b    true
x>y    false
c1==c2  false
35==10 false
...............................................

Example :

class RelationalOperatorDemo{
    public static void main(String[] args){
        int firstVar = 2;
        int secondVar= 9;
        if(firstVar == secondVar)
            System.out.println("firstVar == secondVar");
        if(firstVar != secondVar)
            System.out.println("firstVar != secondVar");
        if(firstVar secondVar)
            System.out.println("firstVar secondVar");
        if(firstVar secondVar)
            System.out.println("firstVar secondVar");
        if(firstVar <= secondVar)
            System.out.println("firstVar <= secondVar");
    }
}

The Output will be 

firstVar != secondVar
firstVar <  secondVar
firstVar <= secondVar
Read more ►

Sunday, August 19, 2012

Bitwise Operators In Java

0 comments
Bitwise operators are used to manipulate individual bits of a data item. There are situations where individual bits of data are modified.

These operators operate only on byte,short ,int and long types 





      OPERATOR                                    DISCRIPTION  




  1.        ~                        ::             Bitwise NOT                         
  1.        &                        ::             Bitwise AND                         
  1.         |                        ::             Bitwise OR                           
  1.         ^                       ::             Bitwise exclusive OR           
  1.        <<                      ::             Left shift                                 
  1.        >>                      ::             Right shift                               
  1.        >>>                    ::             Right shift zero fill                
  • & operator  Bitwise  AND operator  compares each bit of first operand with each bit of second operand.For instance 71 binary number is 01000111 and 25 binary number is 00011001. If both the bits are 1 and 1 the result is 1 if any one of the bit is 0 the result is 0
  • | operator     Bitwise  AND operator  compares each bit of first operand with each bit of second operand. If both the bits are 1 and 1 the result is 1 ,even if any one of the operand is 1 the result is 1 and only if both of the bit is 0 the result is 0
  • ^ operator       Bitwise exclusive or operator  compares each bit of first operand with each bit of second operand. If both  the operand are 0 and 1 the result is 0 and only if any one of the bit is 0 and other is 1 the result is 0
  • ~ Operator     Bitwise NOT operator is a unary operator . This oprator is used only on a single operand.
  • << Operator  The  left shift operator is used to shift the bits of a given operand  to left for specified number of position
  • >> Operator  The  right shift operator is used to shift the bits of a given operand  to right for specified number of position
  • >>> Operator  The  right shift operator is used to shift the bits of a given operand  to right for specified number of position.The  difference in >> and >>>  is the sign bit extension is not done .Instead of 1s filled at leftmost bit is -ve integer values,0's  are filled





public class BitwiseOpertorDemo{
public static void main(String args[]) {
int firstVar = 60; /* 60 = 0011 1100 */
int secondVar = 13; /* 13 = 0000 1101 */
int ThirdVar = 0;
ThirdVar = firstVar & secondVar ; /* 12 = 0000 1100 */
System.out.println("firstVar & secondVar = " + ThirdVar );
ThirdVar = firstVar | secondVar ; /* 61 = 0011 1101 */
System.out.println("firstVar | secondVar = " + ThirdVar );
ThirdVar = firstVar ^ secondVar ; /* 49 = 0011 0001 */
System.out.println("firstVar ^ secondVar = " + ThirdVar );
ThirdVar = ~firstVar ; /*-61 = 1100 0011 */
System.out.println("~firstVar = " + ThirdVar );
ThirdVar = firstVar << 2; /* 240 = 1111 0000 */
System.out.println("firstVar << 2 = " + ThirdVar );
ThirdVar = firstVar >> 2; /* 215 = 1111 */
System.out.println("firstVar >> 2 = " + ThirdVar );
ThirdVar = firstVar >>> 2; /* 215 = 0000 1111 */
System.out.println("firstVar >>> 2 = " + ThirdVar );
}
}
Read more ►