Abstract class in Java programming language
Abstract class in Java programming language
Abstract class
In this tutorial, we will discuss Abstract class in Java programming language
In Java programming language, Abstraction is one of the OOP concepts. A class that is declared as abstract is called as an abstract class.
The abstract class can have abstract and non-abstract methods
Why need abstract
Abstraction is a process of hiding the implementation details( internal detail) from user and user can using only its functionality
Declaration
The syntax of abstract class
abstract class class_name{
}
Example of abstract class
abstract class Calculation{
}
//abstract is a keyword
//abstract classes have abstract and non-abstract methods
//implementation of abstract methods is in another class
abstract method
A method that is declared as abstract and does not have an implementation. It is just a signature.
Declaration
The syntax of the abstract method
abstract return_type method_Name{
}
Example of an abstract method
abstract void get_Display{
}
Points to remember
- An abstract class must be declared with an abstract keyword
- the abstract method has no implementation.
- A class contains at least one abstract method.
- it cannot be instantiated
- An abstract class can have a final method, constructor and static method
- For the class to be an abstract class, one or more methods must be abstract inside the class.
What is the usage of the abstract class
Abstraction hides implementation details and only presents the features to users.
Examples
Program 1
abstract class abstract_1{ abstract void run(); //abstract method only declaration no definition void print() { System.out.println("non abstract method in Java"); } }// abstract_1 class end class Example_1 extends abstract_1{ void run()//definition of abstract method { System.out.println("abstract method in Java"); } public static void main(String args[]){ Example_1 obj=new Example_1(); obj.run(); obj.print(); } }
When the above code executed, it will produce the following output
abstract method in Java non abstract method in Java
Program 2
abstract class myschool{ myschool(){ System.out.println("This is my first school"); } abstract void subject();//abstract method void books() //Normal method { System.out.println("This is my books"); } } class newstudent extends myschool{ void subject(){ //implement abstract method System.out.println("There are ten subject"); } public static void main(String args[]){ myschool obj=new newstudent(); //create an instance of the class obj.books(); obj.subject(); } }
When the above code executed, will be produced following output
This is my first school This is my books There are ten subject
Program 3
abstract class my_college{ // abstract class my_college(){ //constructure System.out.println("This is my college"); }//end of constructure abstract void maths();//Absrract method void medicine(){ //Non abstract method System.out.println("medicine is my favorite course"); } } class my_course extends my_college{ //my_course inherit property of my_college void maths() {//implement of abstract method System.out.println("maths courses are two divisions"); } } class Test_college { public static void main(String args[]){//main method my_college obj=new my_course();// create object obj.maths(); // calling method using object obj.medicine(); } }
When the above code executed, will produce the following output
This is my college Maths courses are two divisions medicine is my favorite course
Abstract class can be contains ,data members and methods
An abstract class can have data members, abstract method, non-abstract method, constructor.
abstract class School{ School()//constructor { System.out.println("This is a constructor inside the abstract class"); } abstract void teachers();//abstract method with no implementation void students(){//non abstract method System.out.println("We are students inside the school"); } } class Principal extends School{//inherit properties from parent class void teachers(){//implements the abstract method System.out.println("We are teachers inside the school"); } } class Society{//this is a test class public static void main(String args[]){//main method School obj=new Principal();//create instance for class obj.teachers();//call method teachers(abstract method) obj.students();//call method students(non abstract method) } }
When the above code executed, will produce the following output
This is a constructor inside the abstract class We are teachers inside the school We are students inside the school
There are other Java language keywords that are similar to this post
Suggested for you
Encapsulation in Java language
Abstract class in Java languag
Method overloading in Java language
Method overriding in Java language
Constructor overloading in Java language
difference to constructor and method in Java
Encapsulation in Java language