Abstraction
Abstraction
===========
There are two types of methods
a. Normal methods
b. Abstract methods
abstract methods:
The abstract method contains only method declaration but not implementation.
The abstract method must ends with semicolon.
Represent the method is abstract using abstract modifier.
Based on above representation of methods the classes are divided into two types,
1) Normal classes.
2) Abstract classes.
abstract class:
case 1: The class contains at least one abstract method is called abstract
class.
abstract class Test
{ abstract void add();
abstract void mul();
abstract void div();
}
Note : The abstract class may contains abstract methods or may not contains
abstract methods but for the abstract classes object creation is not allowed.
Code style-1
abstract class Services
{ abstract void add(int num1,int num2);
abstract String login(String username,String password);
}
class TestClient
{ public static void main(String[] args)
{ // Services s = new Services(); error: Services is abstract; cannot
be instantiated
Note: The abstract class can hold all child classes objects.
Code style-2
ex 2:
abstract class Operations
{ abstract void add(int a,int b);
abstract void mul(int a,int b);
}
class TestClient
{ public static void main(String[] args)
{ Dev2 d = new Dev2();
d.add(10,20);
d.mul(4,5);
}
}
ex-3:
Observatios:
case 1:
abstract class Message
{ abstract final void morn(String name);
}
error: illegal combination of modifiers: abstract and final
case 2:
abstract class Message
{ abstract static void morn(String name);
}
error: illegal combination of modifiers: abstract and static
case 3:
abstract class Message
{ abstract private void morn(String name);
}
error: illegal combination of modifiers: abstract and private
Note: abstract methods we have to override that methods in child classes but
final,static, private methods not possible to override so the combination of
modifiers are illegal.
ex:
abstract class Demo
{ Demo()
{ System.out.println("Abstract class: Demo Constructor");
}
}
class Test extends Demo
{ Test()
{ super();
System.out.println("Normal class constructor ");
}
public static void main(String[] args)
{ new Test();
}
}
Note:
Inside the abstract class it is possible to declare the constructors.
It is not possible to execute the abstract class constructor by creating object of
abstract class. But it is possible to execute the abstract class constructor
indirectly by calling from other child classes.
In above example abstract class constructor executed but object is not created.
Abstraction Definition:
Data abstraction is the process of hiding certain details and showing only
essential information to the user.