wellnwill

abstract keyword


Showing the set of services to the end user and hiding the internal implementation is called Abstraction. In other word, provide the services to the users without showing the background details.

Ex- We can talk to people using a cell phone. We know how to call using cell phone but there is no need to know how is this call service implemented.

There are two ways to achieve abstraction in java:

  • By abstract class
  • By interface

Abstract is a modifier and can be used in the following ways:

  • To define abstract class
  • To define abstract method

We cannot use abstract modifier with variables

Abstract method

  • A method which has both declaration and implementation is known as concrete method or normal method or complete method.
  • A method which has only declaration and no implementation is known as abstract method.
  • Abstract method should be declared with abstract keyword and ends with semicolon. Syntax: abstract return_type method_name (method parameters); Ex- abstract public void m1(); abstract void m2 ();
  • By declaring abstract method in the parent class we can provide guidelines to the child classes that which method is compulsory to child classes need to implement.

Characteristic of abstract method

  • It is always declared inside abstract class.
  • It cannot be invoked by using super keyword from child class.
  • Abstract method has no implementation if any modifier is having implementation then it will be illegal combination with abstract modifier.
  • We cannot use final keyword with abstract method.
  • We cannot use static modifier with abstract method.
  • The responsibility of child class is to provide the implementation for parent class abstract method otherwise we have to declare child class as abstract.

Abstract class

  • A class is said to be abstract if it is defined by using abstract keyword.
  • Ex- abstract class A{}
  • A class which has at least one abstract method then it is mandatory to declare class as abstract.
  • Abstract class can contain only abstract methods or only normal methods or both abstract and normal methods.
  • Abstract class can contain zero or any number of abstract method.
  • We cannot create object of abstract class.
  • If a class has only static methods then it is better to declare class as abstract.
  • Abstract class can contain constructor but interface cannot contain constructor.
  • Abstract class should not be final.
  • We can achieve 0 to 100% abstraction through abstract class because it contains both normal and abstract methods.
  • We can achieve runtime polymorphism through abstract class i.e. the reference variable of parent class can be used to hold the object of child class.

How to access non static normal method present in abstract class

  • Create a subclass which extends abstract class.
  • In sub class override and complete all the parent class abstract methods.
  • Create an object of subclass and call the method.
  • In case of abstract class, we cannot create direct object but we can create indirect object i.e. we can hold child class object into parent class reference variable but we cannot call newly created methods of child class by using that reference
abstract class A
{
 // abstract final void test();
 // abstract static void test();
 A()
 {
  System.out.println("cons of A");
 }
 abstract void test();
 void display()
 {
  System.out.println("display() of A");
 }
}

class B extends A
{
 B()
 {
  System.out.println("cons of B");
 }
void display()
 {
  System.out.println("display() of B");
  super.display();
 }
 void test()
 {
  System.out.println("test() of B");
  //super.test();
 }
}
class Abstract
{
 public static void main(String args[])
 {
  //A a=new A();
  A a=new B();
  a.display();
  a.test();
  B b=new B();
  b.display();
  b.test();
 }
}

Output-
cons of A
cons of B
display of B
display of A
test() of B
cons of A
cons of B
display of B
display of A
test() of B







abstract class BookMyShow
{    
abstract void getFreeMovietickets();    
}    
class ICICI extends BookMyShow
{    
void getFreeMovietickets()
{
System.out.println("2 movie tickets free/month");
}    
}    
class HSBC extends BookMyShow
{    
void getFreeMovietickets()
{
System.out.println("1 movie tickets free/month");
}   
}    
class Test{    
public static void main(String args[])
{ 
BookMyShow b;   
b=new ICICI();
b.getFreeMovietickets();     
b=new HSBC();
b.getFreeMovietickets();   
}
}
Note-Generally we do not know implementation classes and object of implementation class will be provided by 
factory method.     


interface M
{  
void m1();  
void m2();    
}  
abstract class M1 implements M
{  
public void m1()
{
System.out.println("m1() method body");
}  
}  
  
class M2 extends M1
{  
public void m2()
{
System.out.println("m2() method body");
}   
}  
  
class Test{  
public static void main(String args[]){  
M e=new M2();  
e.m1();  
e.m2();  
}
}
class Test3
{
 abstract int i=10;// variable as abstract not applicable
 public static void main(String args[])
  {
   Test3 t1=new Test3();//cannot create object of abstract class
   System.out.println(t1.i);
  }
}

abstract class Test
{
 static void m1()
  {
   System.out.println("m1() body");
  }
 
 static void m2()
  {
   System.out.println("m2() body");
  }
}
class Test2
{
 public static void main(String args[])
  {
   Test.m1();
   Test.m2();
  }
}


abstract class A
{
 abstract void m1();
}
class Child extends A
{
public void m1()
 {
  System.out.println("m1() body");
  super.m1();
 }
}
class Test{
 public static void main(String args[])
 {
   Child c1=new Child();
   c1.m1();
  }
}



Footer with Map

Top