Professional Documents
Culture Documents
The abstract class provides the abstraction level from 1 to 100%. The level
of abstraction is totally depending upon the abstract methods. If an abstract
class has an abstract method and non-abstract method, then the abstraction
level lies between 1 to 100. But if an abstract class contains only abstract
methods then it provides 100% abstraction. We can use any number of
abstract methods in the abstract class as per the use.
An abstract method in java and the use of abstract method in java. Many
programmers have a common question if there is an abstract method in a
class then? So let’s start, The abstract method is part of abstraction in java.
A method declared without a body within an abstract class/interface is an
abstract method. We declare the abstract method only when we require
just method declaration in Parent classes and implementation will provide by
child classes. We can declare an abstract method by use of the abstract
keyword. This is a special type of method because it doesn’t have a body. An
abstract method is declared either within the abstract class or interface. You
can’t declare an abstract method in concrete class. To declare an abstract
method, we have to follow the syntax:
Explanation of Example: Let’s say a programmer want to print the data from
different school but he doesn’t know want will be output from different schools.
So, he/she declares an abstract method in an abstract class. So that each
derived class can provide implements its body according to its functionality. In
this example we have three classes, one is abstract class Data and two class
is Concrete/non-abstract. Data is an abstract class having one abstract
method showData().
The abstract method showData() in-type Data can only set a visibility
modifier, one of public or protected.
}
Output: This will show compile time exception.
Example:
abstract method: Each abstract class has at least one abstract method.
}
}
public class MainClass
{
public static void main(String arg[])
{
StudentRecord studentRecord = new StudentRecord();
studentRecord.courseDetail();
studentRecord.studentData();
HostelRecord hostelRecord = new HostelRecord();
hostelRecord.courseDetail();
hostelRecord.studentData();
}
}
class Student
{
int rollNo;
String name;
String className;
public Student(int rollNo, String name, String className) {
this.rollNo = rollNo;
this.name = name;
this.className = className;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
Let’s say, I am the principal of a college and I want details of each department
of the college. My college has various departments like Art, science,
commerce, etc. I want to know the department name, how many teachers are
in each department(mandatory information), and some additional information.
So, I will create a structure that will be followed by each department. I will
create an abstract class with an abstract method that will be inherited by
each department. So each department must provide the implementation of the
abstract method.
Here we have three classes one class is abstract (Department) and the other
two are concrete classes(Art class and science class). These concrete
classes inherit the abstract class so they must provide the implementation of
abstract methods.
{
String collegeName = "KUK";
public void collegeName()
{
System.out.println("Name of college = "+ collegeName);
}
//abstract methods
public abstract void deptName();
public abstract void noOfTeachers();
}
class ArtDepartment extends Department
{
public void showData()
{
System.out.println("This is Art department's data");
}
@Override
public void deptName()
{
System.out.println("Department name is Art");
}
@Override
public void noOfTeachers()
{
System.out.println("Number of teacher are 10");
}
}
class ScienceDepartment extends Department
{
public void getStudentsData()
{
System.out.println("This data is belongs to science students");
}
@Override
public void deptName()
{
System.out.println("Department name is Science");
}
@Override
public void noOfTeachers()
{
System.out.println("Number of teacher are 20");
}
}
class MainClass
{
public static void main(String arg[])
{
// Displaying information of different departments
ArtDepartment artDepartment = new ArtDepartment();
artDepartment.showData();
artDepartment.deptName();
artDepartment.noOfTeachers();
artDepartment.collegeName();
System.out.println("");
ScienceDepartment scienceDepartment = new ScienceDepartment();
scienceDepartment.getStudentsData();
scienceDepartment.deptName();
scienceDepartment.noOfTeachers();
scienceDepartment.collegeName();
}
Output:
This is Art department’s data
Department name is Art
Number of teacher are 10
Name of college = KUK
This data is belongs to science students
Department name is Science
Number of teacher are 20
Name of college = KUK
Firstly, we will discuss the problem area. After that we will find the solution with
abstract class.
Let’s say we have a class Company that has two methods companyDetail()
and employeeDetail(). The companyDetail() method contains information
about the company that is common for all employees and employeeDetails()
method contains information about employees which differs for every
employee. We have three subclasses for employees that are Developer,
Manager and IT. Since each employee has different personal information and
salary. So, every subclass overrides the employeeDetails() method to give
the details of its own implementation. Here you must have noticed, the
employeeDetails() method in Company class (Parent class) is not creating
any value because every child class overrides the method.
class Company
{
public void companyDetail()
{
System.out.println("Company name : Final Rope");
}
public void employeeDetail()
{
System.out.println("Employee name : Ram");
System.out.println("Employee ID : 101");
System.out.println("Employee Salary : XXXXX");
}
}
class Developer extends Company
{
public void employeeDetail()
{
System.out.println("Employee name : Sham");
System.out.println("Employee ID : 201");
System.out.println("Employee Salary : XXXXX");
}
}
class Manager extends Company
{
public void employeeDetail()
{
System.out.println("Employee name : Krishan");
System.out.println("Employee ID : 301");
System.out.println("Employee Salary : XXXXX");
}
}
class IT extends Company
{
public void employeeDetail()
{
System.out.println("Employee name : John");
System.out.println("Employee ID : 401");
System.out.println("Employee Salary : XXXXX");
}
}
public class MainClass
{
public static void main(String arg[])
{
Developer developer = new Developer();
developer.companyDetail();
developer.employeeDetail();
Manager manager = new Manager();
manager.companyDetail();
manager.employeeDetail();
IT it = new IT();
it.companyDetail();
it.employeeDetail();
}
}
No, An abstract class can’t be final because the final and abstract are
opposite terms in JAVA.
Reason: An abstract class must be inherited by any derived class because a
derived class is responsible to provide the implementation of abstract methods
of an abstract class. But on another hand, if a class is a final class, then it
can’t be extended(inherited). So both concepts are opposite to each other.
abstract final class Department
}
//abstract methods
public abstract void deptName();
public abstract void noOfTeachers();
}
class MainClass extends Department
{
public static void main(String arg[])
{
MainClass obj = new MainClass();
}
@Override
public void deptName() {
// TODO Auto-generated method stub
}
@Override
public void noOfTeachers() {
// TODO Auto-generated method stub
}
As you can see in the above example, We are trying to use two opposite
keywords (abstract and final) for a single class. In this case, the compiler
shows an error compile-time, because Java doesn’t permit it.
Because if the compiler works according to the final keyword then the
Department class must not be extended by any other class. But the abstract
keyword is vice-versa. It means according to abstract keyword the
Department must be extended by some other class. To remove this ambiguity
of compiler, Java doesn’t permit it.
No, we can’t create an object of it. As know abstract class is not a complete
class. because an abstract class has abstract method (Methods without body).
Although an abstract class has a constructor if you will try to create an object
of it, It will throw compile time exception.
You have seen in the above example the compiler throws the exception at
compile time. The exception message is not directly saying that you can’t
create an object it. It just says “Cannot Instantiate The Type
ExampleOfAbstractClass”, which means that you cannot create an object of
it.
You must think about the constructor of the abstract class. If we can’t create
the object of it then what is the use of a constructor in the abstract class? The
constructor of the abstract class is to initialize the fields which belong to the
abstract class. But this constructor is invoked from a concrete subclass as part
of creating an instance of a concrete subclass in Java.
If java allows creating an object of it, then it will lead to two problems:
1. The first problem occurs because the abstract method doesn’t provide the
body of the method. They are just declared in the abstract class. If someone
calls the abstract method using that object, then What would happen?
2. Abstract classes are only like a template, to create an object of any class,
the class should be concrete because an object is concrete.
An abstract class can have abstract methods and we can’t create its object.
You can read “Why we can’t create an object of the abstract class?” But
here a question strike in mind, Can abstract class have a constructor? If
yes, then why and what is the use of constructor in abstract even we can’t
create the object of an abstract class. Here we will discuss it in detail.
Yes, an abstract class can have a constructor in Java. The compiler
automatically adds the default constructor in every class either it is an
abstract class or concrete class. You can also provide a constructor to
abstract class explicitly. The constructor is always used to initiate a class or
use it to create an object of the class. But we can’t instantiate it with the new()
operator or any other ways.
By default, JVM is dealing with the constructor chaining because if you are
creating an object by a new keyword then JVM invokes the constructor of
that class and that constructor invokes the superclass constructor. If you want
to know how constructor chaining works, then read the article.
To use an abstract class in Java, we use the concrete class that extends the
abstract class and provides the implementation to the abstract methods.
When we use the constructor of the child class, the constructor of the parent
class invoked by child class constructor either implicitly or explicitly. This is
one of the reasons abstract class can have constructors in Java. Suppose if
JVM doesn’t provide the default constructor to an abstract class then JVM not
able to support constructor chaining.
As you know a constructor is used to initialize the fields of the class. Suppose
we want to set some common behavior for all the child classes and some
behavior depends upon the subclass implementation. Then we can use the
constructor of the abstractor class to initialize the fields.
Let’s suppose we have an abstract class Car that has some common
properties. These common properties will be common for each subclass that
extends the Car class. We can initialize these fields in the Constructor of
abstract class so, when you instantiate the constructor of child classes then
the parent constructor will be called, and the fields will be initialized.