OOPS in JAVA 19
Abstract class in Java
If a class contain any abstract method then the class is declared as abstract class. It can have abstract and non-abstract methods (method with body).
Abstraction in Java:
Abstraction is a process of hiding the implementation details and showing only functionality to the user. Abstraction lets you focus on what the object does instead of how it does it.
Abstraction in Java:
1. Abstract class (0 to 100%)
2. Interface (100%)
Abstract class:
A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.
Syntax:
abstract class class_name {
}
Abstract method:
Method that are declared without any body within an abstract class 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
NOTE: Instead of curly braces({ }) an abstract method will have a semicolon( ; ) at the end.
Example of Abstract class
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();
}
}
Output : this is callme.
Points to Remember:
1. Abstract classes are not Interfaces. They are different, we will study this when we will study Interfaces.
2. An abstract class must have an abstract method.
3. Abstract classes can have Constructors, Member variables and Normal methods.
4. Abstract classes are never instantiated.
5. When you extend Abstract class with abstract method, you must define the abstract method in the child class, or make the child class abstract.
When to use Abstract Methods & Abstract Class??
Ans) Abstract methods are usually declared where two or more subclasses are expected to do a similar thing in different ways through different implementations. These subclasses extend the same Abstract class and provide different implementations for the abstract methods.
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )