.
Java Abstract Class and Method (With Example)
What is Abstraction in Java?
- Abstraction is one of the important oops concepts,
- Abstraction is defined as the process of showing the set of services and hiding the implementation.
Example of Abstraction in Java
here a real-time example of abstraction is a Bank ATM In an ATM machine the screen shows the set of services like Cash withdrawal, transfer, and change PIN but it does not show the internal implementation, this is all about abstraction.
We are achieving the abstraction by using abstract class and interface.
Using abstract class (0% to 100%)
Using interface (100%)
Before learning abstraction first, we have to understand the abstract class and abstract method.
Abstract Method
The abstract method in Java is declared with an abstract keyword and will have a semicolon "; " at the end.
An abstract method is a just a signature without any implementation block inside. abstract method must be overridden in the sub-class to make use of the object to invoke.
An abstract method is just a prototype for the method with the following attribute.
1) A return type
2) A name
3) A list of Parameters
4) A throws clause which is optional
Syntax of abstract Method:
abstract void salary(); //This is the example of abstract method
Following is an example of the abstract method
Abstract Class
In Java An abstract class is defined as the "Class may contain abstract method or may not contain abstract method but for the abstract classes object creation not possible ".
A class that is declared with an abstract keyword is known as an abstract class.
Important Points to Remember
Case:1 Class contain at least one abstract method
Case:2 abstract Class does not contain abstract method
Example:1 Abstract Class Example with object Creation
Example -2 Inside the abstract Class declarations of main method is possible
Hope !! The above Tutorial Abstraction in Java With Example is helpful For you... ...
- The abstract method contains only method declaration but no implementation.
- Every abstract method must end with a semicolon(;)
- We use an abstract modifier to represent a method is abstract.
Following is an example of the abstract method
public abstract class School//Abstract Class
{
String name;
int rollnumber;
abstract void Student ();//Abstract Method
}
In Java An abstract class is defined as the "Class may contain abstract method or may not contain abstract method but for the abstract classes object creation not possible ".
A class that is declared with an abstract keyword is known as an abstract class.
Important Points to Remember
- For the abstract classes object creation is not possible.
- Abstract Class may contain abstract methods or may not contain abstract methods.
- Abstract class must declare with abstract modifier.
- if the class contain the abstract method then write the implementation in child classes.
- Inside the abstract class, it is possible to declare the main method.
- Inside the abstract Class, it is possible to declare the constructor.
- it is possible to override the abstract method to the normal method.
- it is possible to override the normal method to the abstract method.
- Inside the abstract class, it is possible to declare the variable.
- Inside the abstract class, it is possible to declare the instance block and static block.
- In the Child class, it is possible to override the abstract method and declare the user-defined normal method.
Case:1 Class contain at least one abstract method
public abstract class School //Abstract Class
{
String name;
int rollnumber;
void student()
{
}
void student()
{
}
abstract void student(); //Abstract Method
}
Case:2 abstract Class does not contain abstract method
public abstract class School//Abstract Class
{
{
String name;
int rollnumber;
void student()
{
}
void student()
{
}
}
Example:1 Abstract Class Example with object Creation
//abstract parent class
abstract class Animal
{
//abstract method
public abstract void sound();
}
//Dog class extends Animal class
public class Dog extends Animal
{
public void sound()
{
System.out.println("huhuhu");
}
public static void main(String args[])
{
Animal obj = new Dog();
obj.sound();
}
}
Example -2 Inside the abstract Class declarations of main method is possible
public abstract class School //Abstract Class
{
public static void main(String args[])
{
System.out.println("Abstract main method ");
}
}
Hope !! The above Tutorial Abstraction in Java With Example is helpful For you... ...
0 Comments