SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

What Is An Abstract Class In Java?

What Is An Abstract Class?
If you will go to attend an interview for selenium WebDriver software testing tool with java , 90% Interviewer will ask you this question. Your answer should be like this : An abstract class is a class that is declared with abstract keyword. If class contains any abstract method then you must have to declare your class as abstract class in java software development language.  Also abstract classes can be subclassed, but they cannot be instantiated. That means you can not create object of abstract class. It may or may not have abstract methods. it can have concrete methods too so that it does not provide 100% abstraction.

Earlier we learnt interface in THIS POST. It is 100% abstraction. View THIS POST to know difference between abstract class and interface.

Bellow given class has abstract method so it is declared as abstract class.

//abstract class
public abstract class Animal {

 //concrete method
 public void eat(String food) {
  // do something with food....
 }

 // abstract method
 public abstract void makeNoise();
}

Let's try to understand abstract class and abstract methods in detail.

What Is Abstraction in Java?
Abstraction is process of hiding implementation details from user in java software development language. User can use only functionality but can not see how it is implemented. Example : Calling a friend is best example of abstraction. Here you can talk with each other but you don't know about internal implementation of your phone and it's network using which you can talk. It is called abstraction.

What Is Abstract Method?
If any method is declared with abstract keyword then it is called abstract method in java software development language. Abstract method can not have a body. Actual implementation of abstract method will be done by it's child class. If any class extends abstract class then that subclass must have to implement all the abstract methods declared by it's super class(abstract class).

Example of abstract class with simple and concrete methods is as bellow.
Animal.java
package abstraction;

//abstract class
public abstract class Animal {

 // concrete method. Sub class can use it if needed.
 public void eat(String food) {
  // do something with food....
 }

 // concrete method. Sub class can use it if needed.
 public static void sleep(int hours) {
  // do something with sleep....
 }

 // abstract method. Sub classes must have to implement it if extend this class.
 public abstract void makeNoise();
}

Bellow given classes are sub classes of  Animal class so both of them have implemented abstract method of Animal abstract class. But they do not need to implement concrete methods of super class. They can declare and implement their own concrete methods independently.

Dog.java
package abstraction;

public class Dog {

 // abstract method of super class is implemented in sub class.
 public void makeNoise() {
  System.out.println("Bark! Bark!");
 }

 // Concrete method
 public void payingBall() {
  System.out.println("Dog is playing with ball");
 }
}

Cow.java
package abstraction;

public class Cow extends Animal {

 // abstract method of super class is implemented in sub class.
 public void makeNoise() {
  System.out.println("Moo! Moo!");
 }

 // Concrete method
 public void milking() {
  System.out.println("Cow is milking");
 }
}

When And Why To Use Abstract Class?
If you see at above example, Abstract class is not only template for it's child classes but it has it's own functionality too. Like eat(String food), sleep(int hours). Both these methods are concrete but child class can use them if required. 

You can use abstract class as parent class for sub classes
  • When you expect to implement same method in all sub classes with different implementation detail.
  • When you expect to implement methods in super class(abstract class) which can be used by its sub classes if needed.
  • When you expect to implement independent method in sub class which do not have any relation or use with its super class.
Points to Remember About Abstract Class
  1. If class is declared with abstract keyword then it is called abstract class in java software development language.
  2. If class has abstract method, you must have to declare class as abstract class.
  3. In abstract class, You can only declare abstract methods but you can not define abstract methods.
  4. Sub classes of abstract class must have to implement all abstract methods of super abstract class.
  5. You can not instantiate(Can not create object of) abstract class.
  6. Abstract classes can have concrete methods, constructors and Member variables too.
I think now you are good with abstract class to use it in selenium webdriver software testing tool.

6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. If i am not wrong Class Dog should extend Class Animal

    ReplyDelete
  3. very useful information....Thank u Aravind G

    ReplyDelete