ABSTRACT CLASS

ABSTRACTION


  • Showing necessary details and hiding unnecessary details (OR) showing what to do,and not showing how to do is called as “Abstraction”.
  • We can achieve abstraction in java through “abstract” keyword. Abstract keyword is used for methods and classes. But we can’t use for attributes, constructors and initialization of blocks.

Abstract for class

  • If we define one class as an abstract we can’t create object to that class. If don’t want to allow creating object to every class then declare the class with “abstract”.
  • Syntax for abstract class :

     
                                
                                  abstract class  A
                                  {
                                      ------------;
                                  }     
                                

Example 1:

 
						
                    abstract class  A 
                    {
                     
                    }
                    class B
                    {
                       public static void main(String args[])
                       {
                          A  a1=new  A();   /*  compilation fail why because   */
                       }                    /*  here class A is an abstract class.   */ 
                    }                       /*  So we can’t create object to that  Abstract class.  */ 
                        

Abstract methods :

  • we use abstract keyword for methods in the cases where we don’t want to define, definition for declaring methods in such cases we go for abstract method. Abstract methods end with semicolon(;).
  • If one class contain at least one abstract method then we should declare the class as abstract.
  • Inside abstract class we can declare both abstract and concrete methods. Concrete methods means non-abstract methods.

Example 2:

 
						
                     abstract class A
                     {
                        abstract void  ram();
                     }
                                            	
Note
  • In abstract class,it may (OR) may not contain the abstract methods.

Example 3:

 
						
                     abstract class A
                     {
                        void ram()
                        {
                           ------------------;
                        }
                     }                       	
Note
  • For abstract methods, we should provide implementation inside sub class. We can implement the abstract methods inside sub class while we can remove the abstract keyword.

Example 4:

 
						
                     abstract class A
                     {
                        abstract void ram();
                        {
                           --------------;
                        }
                     }
                     class B extends A
                     {
                        ------------;    /*  compilation fail why because here one  */
                        --------------;  /*  abstract method inherited from super class  */
                     }                   /*  to sub class. But sub class is not defined  */
                                         /*  as a abstract (OR) abstract method is not   */ 
                                         /*  implemented in sub class.  */                     	
The above example can be solved in two ways.

First Way :

 
						
                     abstract class B extends A
                     {
                        ----------------;
                        -----------------;
                     }
                                          	

Second Way :

 
						
                     class  B extends class A
                     {
                        void ram()
                        {
                           System.out.println(“hello ram”);
                        }
                     }
                                          	

Inside abstract class we can keep as many as constructors. Abstract constructor will be executed whenever we create objects to sub class through super() (OR) constructor chaining. It can only satisfy the constructor chaining.

Example 5:

 
						
                     abstract class A
                     {
                        void m1();
                        {
                           System.out.println(“hello ram”);             
                        }
                        A()
                        {
                           
                        }
                        A(int i)
                        {
                           this();
                        }
                        A(String s)
                        {
                           
                        }
                     }
                     class B extends A
                     {
                        public static void main(String ram[])
                        {
                           B b1=new B();
                           A a1=new B();   /*   We can use abstract class as a   */ 
                        }                  /*   reference variable to point sub   */
                     }                     /*   class object but we can’t point the Same class object. */                     	

  • Even though we give abstract methods we can’t achieve 100% abstractness.
  • Through abstract class we can’t achieve 100% abstraction even though if we keep all abstract methods inside a class.
  • If we want to achieve 100% abstractness then we should not keep any definition block. In this abstract class it is not possible because if we compile abstract class, the compiler creates default constructor. Inside constructor first statement is super( ).
Java Setup Wizard
abstract class complilation.

  • By using “extends “ keyword for class we can’t achieve multiple inheritance.

Example 6:

 
						
                     abstract  class A
                     {
                        abstract void test();
                     }
                     abstract class B
                     {
                        abstract void test();
                     }
                     /*   class C extends A,B   */  
                     /*   Java doesn’t support multiple inheritance.   */
                     /*   Compiler will be confused either “A “is super or “B” is super class constructor.   */
                     /*   we have to execute from subclass “C” constructor.   */
                     {
                     }                     	

  • Due to constructor chaining we will get ambiguity error as super class constructor executes from sub class.
  • In order to avoid drawbacks of “extends “ keyword sun people came up and introduced new concept i.e interfaces.

Example 4:

 
						
                     interface  A
                     {
                        int i;
                        /*  Compilation fails at line no3. Because  every           */
                        /*  attribute inside interface is implicitly final,         */
                        /*  so we should assign a value while declaring attributes. */
                        int test();
                     }
                     interface B
                     {
                        int j=10;
                        int test();
                     }
                     abstract  interface  C extends  A,B
                     {
                        void test();
                     }
                     /*  abstract class D extends C  */
                     /*  {   */
                     /*  }   */
                     /*  Compilation fails  from line no 15_19.Because  A class  */
                     /*  can implement an interface but can’t extend an interface.*/
                     class E
                     {
                        void test()
                        {
                           System.out.println(“C”);
                        }
                     }
                     /*  class  F  extends  E implements C  */
                     /*  {  */
                     /*  }  */
                     /*  Compilation fails from line no 27-31 . because of either                  */
                     /*  there is no implementation  for      test() inside “class F”              */
                     /*  as it expands “interface C”  or “class F” isn’t declared as an abstract.  */
                     abstract  class Y extends E implements C
                     {
                     }
                     class Z extends  E implements C
                     {
                        /*  protected  int test() ;  */
                     }
                     /*  Compilation fails at line no 39 Because  test() is an            */
                     /*  abstract method inside  “interface C” . by default               */
                     /*  evry method  access level is “public” inside interface .         */
                     /*  While overrding we must provide same access level or wider       */
                     /*  access level ,But here “public” is the wider most access         */
                     /*  level,so we must use public access level only while implementing */
                     /*  it.We should not use any other narrow  type access level like    */
                     /*  private/default/protected.*/
                     {
                        System.out.println(“ABC”);
                     }                        
                       	

   Output:

forest is empty Forest has void type plants:20 forest has int with integer type plants.30.25 forest has int & double type plants: 10 forest has double with string type plants:tamrind forest has int & double type plants: 20
Note
  • Parameter values may or may not be used.
  • We can use parameter also as a return value.

Example 5:

 
						
                     public class Vehicle
                     {
                        int i;
                        void bike()
                        {
                           System.out.println("2 wheeler");
                        }
                        void bike(int k)
                        {
                           i=k+20;
                        }
                        float bike(float f)
                        {
                           float f1;
                           f1=f+20.25f;
                           return 10.77f;
                        }
                        /* int bike(int l) */
                        /* {  */
                        /*   i=l+30;  */
                        /*   return 100;  */
                        /* }  */
                        
                        /* Here compilation fails  because of method name and signature type */
                        /*   & order is same for two methods but return type is different. */
                        /* Method return type is not a part of the method signature. */
                        void car()
                        {
                           System.out.println("4 wheeler"); 
                        }
                        Vehicle car(double d)
                        {
                           System.out.println("hi speed car"); 
                           return null; 
                        }
                        void car(int i,float f)
                        {
                           System.out.println("int-float type car");   
                        }
                        void car(float f,int i)
                        {
                           System.out.println("float-int type car");
                        }   
                        int car(double d,long l)
                        {
                           System.out.println("double-long type cars");
                           return 40;   
                        }
                        /* void car(int j,float k)  */
                        /* {  */
                        /* System.out.println("int-float type cars");  */
                        /* }  */
                     
                        /* Here compilation fails because the two methods have  */
                        /* same method name and signature type & order.  */
                        public static void main(String[] args) 
                        {
                           Vehicle v=new Vehicle();
                           v.bike();
                           v.bike(25);
                           v.bike(30.20f);
                           v.car();
                           v.car(50.55);
                           v.car(10,20.20f);
                           v.car(30.30f,50);
                           v.car(25.20,40);
                        }
                     }                        
                       	

   Output:

2 wheeler int type bike float type bike 4 wheeler hi speed car int-float type car float-int type car double-long type cars