By using this concept we can able to hide the main functionality and shown only what the implementation does. Like in the smartphone we don't know how the feature is implemented but the user can able to see and use the outcome for those implementations. In Encapsulation, data will be hidden and in abstraction, implementation will be hidden.
By using Interface, abstract classes, and abstract methods we can able to achieve this. By using this concept, can able to identify only the required characteristics and can omit unwanted implementations.
Key points to note
- If a class is a method that is declared as Abstract without any implementation then it will become an abstract class and abstract method.
- If a class contains any abstract method, the class should be declared as abstract
- If any class inherits an abstract class, it should give implementation for all the abstract methods of the parent class or make the child class itself an abstract
- Should not able to create object reference for abstract class.
- It avoids complexity and increases reusability
Using abstract classes and abstract methods
Sample program
------------------------------------------------------------------------------------------------------------------------
package oops;abstract class StudentDataBase{abstract void StudentName(String a);void studentSection() {System.out.println("Student sectioon is A");}}public class Student extends StudentDataBase {@Overridevoid StudentName(String a) {System.out.println("The student name is - " +a);}public static void main(String[] args) {Student p = new Student();p.StudentName("Ram");p.studentSection();}}
------------------------------------------------------------------------------------------------------------------------
Output:
The student name is - Ram
Student section is A
Program Explanation
In the above-mentioned program, Class StudentDataBase had an abstract method StudentName. So, this class will be an abstract class. And another class Student inherits StudentDataBase and provides the implementation for the abstract method StudentName. So, the Student class will not be an abstract class.
For example, let's take an abstract class Class Play having 2 abstract method Score and Scoreboard. Had one concrete method gotInjured which had some implementation in it. Then we had the other 2 classes Cricket and Football that inherit Class Play. Class Cricket and Football had its own implementation for the parent class abstract methods Score and Scoreboard.
Hope this will be helpful for you.
Don't forget to post your question and suggest to us to improve.
Comments
Post a Comment