Abstract class in java

A class that is declared with abstract keyword, is known as abstract class in java.
  • It can have abstract and non-abstract methods. 
  • If a class has at least one abstract method then the class must be declared as abstract . 
  • abstract class can't be initiated .
  • To use an abstract class , you have to extends it to another class and must be implement all the abstract method . 

Example :

  abstract class Bank{   
        abstract int getBalance();   
    }

  class Savings extends Bank{
  
        int getBalance(){return 40;}   

   }

  class Current extends Bank{

         int getBalance(){return 200;}

   }   
   
class Test{   
     public static void main(String args[]){   
          Bank b; 
            b=new Savings(); 
            System.out.println("Savings Balance is: "+b.getBalance());   
            b=new Current(); 
            System.out.println("Current Balance is: "+b.getBalance());   
        }
   }

Output : 

              Savings Balance is: 40
              Current Balance is: 200


           

Find us :
        Facebook : @apnaandroid
        Google+   : Apna Java
        Youtube : Android & Java Tutorial

Comments

Popular posts from this blog

Disable/Hide Year from DatePickerDialog in android

Custom Calendar in android

Constructor in Java