What Is An Abstract Class?
If y'all volition become to attend an interview for selenium WebDriver software testing tool amongst coffee , 90% Interviewer volition inquire y'all this question. Your response should endure similar this : An abstract class is a shape that is declared amongst abstract keyword. If shape contains whatsoever abstract method as well as thence y'all must accept to declare your shape equally abstract shape inward coffee software evolution language. Also abstract classes tin plow over the sack endure subclassed, but they cannot endure instantiated. That way y'all tin plow over the sack non create object of abstract class. It may or may non accept abstract methods. it tin plow over the sack accept concrete methods also thence that it does non supply 100% abstraction.
Earlier nosotros learnt interface inward THIS POST. It is 100% abstraction. View THIS POST to know divergence betwixt abstract shape as well as interface.
Bellow given shape has abstract method thence it is declared equally abstract class.
//abstract class world abstract class Animal { //concrete method world void eat(String food) { // exercise something amongst food.... } // abstract method world abstract void makeNoise(); }
Let's endeavour to sympathize abstract shape as well as abstract methods inward detail.
What Is Abstraction inward Java?
Abstraction is procedure of hiding implementation details from user inward coffee software evolution language. User tin plow over the sack utilization entirely functionality but tin plow over the sack non run into how it is implemented. Example : Calling a friend is best instance of abstraction. Here y'all tin plow over the sack utter amongst each other but y'all don't know virtually internal implementation of your telephone as well as it's network using which y'all tin plow over the sack talk. It is called abstraction.
What Is Abstract Method?
If whatsoever method is declared amongst abstract keyword as well as thence it is called abstract method inward coffee software evolution language. Abstract method tin plow over the sack non accept a body. Actual implementation of abstract method volition endure done past times it's nipper class. If whatsoever shape extends abstract shape as well as thence that subclass must accept to implement all the abstract methods declared past times it's super class(abstract class).
Example of abstract shape amongst uncomplicated as well as concrete methods is equally bellow.
Animal.javapackage abstraction; //abstract class world abstract shape Animal { // concrete method. Sub shape tin plow over the sack utilization it if needed. world void eat(String food) { // exercise something amongst food.... } // concrete method. Sub shape tin plow over the sack utilization it if needed. world static void sleep(int hours) { // exercise something amongst sleep.... } // abstract method. Sub classes must accept to implement it if extend this class. world abstract void makeNoise(); }
Bellow given classes are sub classes of Animal shape thence both of them accept implemented abstract method of Animal abstract class. But they exercise non demand to implement concrete methods of super class. They tin plow over the sack declare as well as implement their ain concrete methods independently.
Dog.java
package abstraction; world shape Dog { // abstract method of super shape is implemented inward sub class. world void makeNoise() { System.out.println("Bark! Bark!"); } // Concrete method world void payingBall() { System.out.println("Dog is playing amongst ball"); } }
Cow.java
package abstraction; world shape Cow extends Animal { // abstract method of super shape is implemented inward sub class. world void makeNoise() { System.out.println("Moo! Moo!"); } // Concrete method world void milking() { System.out.println("Cow is milking"); } }
When And Why To Use Abstract Class?
If y'all run into at higher upward example, Abstract shape is non entirely template for it's nipper classes but it has it's ain functionality too. Like eat(String food), sleep(int hours). Both these methods are concrete but nipper shape tin plow over the sack utilization them if required.
You tin plow over the sack utilization abstract shape equally nurture shape for sub classes
- When y'all hold off to implement same method inward all sub classes amongst unlike implementation detail.
- When y'all hold off to implement methods inward super class(abstract class) which tin plow over the sack endure used past times its sub classes if needed.
- When y'all hold off to implement independent method inward sub shape which exercise non accept whatsoever relation or utilization amongst its super class.
Points to Remember About Abstract Class
- If shape is declared amongst abstract keyword as well as thence it is called abstract shape inward coffee software evolution language.
- If shape has abstract method, y'all must accept to declare shape equally abstract class.
- In abstract class, You tin plow over the sack entirely declare abstract methods but y'all tin plow over the sack non define abstract methods.
- Sub classes of abstract shape must accept to implement all abstract methods of super abstract class.
- You tin plow over the sack not instantiate(Can non create object of) abstract class.
- Abstract classes tin plow over the sack have concrete methods, constructors as well as Member variables too.