Couple of days back I wrote an article on basic Java Fundamental on What is an Interface in Java and How it’s used? This tutorial is also related to basic Java fundamental “Abstract Class and Abstract Method
“. “What is an Abstract Class?” – This is very basic Java Interview Question. Probably the 1st Java Interview Question you get during interview.
Let’s start understanding Abstract class first and then we will go over Example.
What is an Abstract Class?
- An abstract class is a class that is declared
abstract
- Abstract classes cannot be instantiated
- Abstract classes can be subclassed
- It may or may not include abstract methods
- When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class
- If subclass doesn’t provide implementations then the subclass must also be declared
abstract
.
What is an Abstract Method?
- An abstract method is a method that is declared without an implementation
- It just has a method signature
Let’s start with an Example. Problem Description:
- Create class
CrunchifyExam.java
, which has one abstract method calledcheckResult()
- Create class
Crunchify1stSchoolExamResult.java
, which extends Abstract classCrunchifyExam.java
- Create class
Crunchify2stSchoolExamResult.java
, which extends Abstract classCrunchifyExam.java
- Now both above classes have to provide implementation for checkResult() method
- Both Schools may have their own different procedure or number of checks to find out if user is PASSED or FAILED, they are free to have their own implementation of checkResult()
package com.crunchify.tutorial; import java.util.Date; /** * @author Crunchify.com * */ public abstract class CrunchifyExam { public enum ExamStatus { PASSED, FAILED } private Date examTime; private ExamStatus status; public CrunchifyExam(Date examTime, ExamStatus status) { this.examTime = examTime; this.status = status; } public void getExamTime() { System.out.println("This is your Exam Status: " + examTime); } public void setExamTime(Date examTime) { this.examTime = examTime; } public void setExamStatus(ExamStatus status) { this.status = status; } public ExamStatus getExamStatus() { return status; } abstract public void checkResult(); }
Once you extend CrunchifyExam class in Crunchify1stSchoolExamResult.java
– Eclipse will prompt you to implement Abstract methods mentioned in CrunchifyExam.java
package com.crunchify.tutorial; import java.util.Date; /** * @author Crunchify.com */ public class Crunchify1stSchoolExamResult extends CrunchifyExam { public Crunchify1stSchoolExamResult(Date examTime, ExamStatus status) { super(examTime, status); // TODO Auto-generated constructor stub } @Override public void checkResult() { String studentName = "Nancy"; String studentResult = "85%"; // School NO-1 will provide all their formula to find if user is passed or failed. // After detailed calculation let's say student's grade is "PASSED". ExamStatus examResult = ExamStatus.PASSED; setExamStatus(examResult); System.out.println("Hey.. this is Crunchify's user1 " + studentName + "and here School grade is " + studentResult + " - " + getExamStatus()); } }
package com.crunchify.tutorial; import java.util.Date; /** * @author Crunchify.com */ public class Crunchify2ndSchoolExamResult extends CrunchifyExam { public Crunchify2ndSchoolExamResult(Date examTime, ExamStatus status) { super(examTime, status); // TODO Auto-generated constructor stub } @Override public void checkResult() { String studentName = "John"; String studentResult = "45%"; // School NO-2 will provide all their formula to find if user is passed or failed. // After detailed calculation let's say student's grade is "FAILED". ExamStatus examResult = ExamStatus.FAILED; setExamStatus(examResult); System.out.println("Hey.. this is Crunchify's user1 " + studentName + "and here School grade is " + studentResult + " - " + getExamStatus()); } }
Now you may have a question: Why can’t I use Interface here rather than happing Abstract Method and Class and have CrunchifyExam as an Interface?
Well – Sure you could – but you’d also need to implement the getExamTime
(), setExamTime
(), getExamTime
(), setExamTime
() methods. By using abstract classes, you can inherit the implementation of other (non-abstract) methods. You can’t do that with interfaces – an interface cannot provide any method implementations.
The post What is an Abstract Class and Abstract Method in Java? When Should I use it? Example Attached appeared first on Crunchify.
Author: Arpit Shah