What is Absraction?
Abstraction is process of hiding the implementation details and showing only the functionality.
Hiding of data is known as data abstraction.
We use abstraction every day when interacting with technological objects.
Example :Sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery.
What is Abstract class?
A class that is declared as abstract is known as abstract class.
If a class contain any abstract method then the class is declared as abstract class.
An abstract class is never instantiated. It is used to provide abstraction.
Although it does not provide 100% abstraction because it can also have concrete methods.
Syntax
abstract class class_name { }
What is Abstract method?
A method that is declare as abstract and does not have implementation is known as abstract method.
Any class that extends an abstract class must implement all the abstract methods declared by the super class.
Syntax
abstract return_type function_name(); // No definition
Example
abstract class A{ abstract void callme(); } class B extends A{ void callme(){ System.out.println("this is callme."); } public static void main(String[] args){ B b = new B(); b.callme(); } }Try it Yourself
Can Abstract class have concrete(normal) method?
Yes,Abstract class can also have normal methods with definitions, along with abstract methods.
Example
abstract class A{ abstract void callme(); public void normal(){ System.out.println("this is concrete method"); } } class B extends A{ void callme(){ System.out.println("this is callme."); } public static void main(String[] args){ B b = new B(); b.callme(); b.normal(); } }Try it Yourself
Solve this!!
Abstract class cannot be directly initiated with new operator, Since abstract class does not contain any definition of implementation it is not possible to create an abstract object.
class A { int i; void display() { System.out.println(i); } } class B extends A { int j; void display() { System.out.println(j); } } class method_overriding { public static void main(String args[]){ B obj = new B(); obj.i=1; obj.j=2; obj.display(); } }
- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling