Abstract Class in Java and Methods [With Examples]

The following article will discuss the properties of abstraction in Java and how it could be achieved through abstract classes and abstract methods. We will be exploring the features of abstract classes and interfaces in java. 

Abstraction

One of the important concepts of Java is abstraction. It is the property by which the user is displayed with only the important details of a program. All the non-essential parts are kept hidden. The process can be explained through the example of an email. The senders send an email which gets delivered to the receiver. The only information known to the sender is that the email has been sent. The background process of sending the email is hidden from the users. Similar is the process in Object-Oriented Programming (OOP) where essential elements are displayed to the user while the non-essential parts are hidden. The user has no information about the implementation process, but only the functionality is known. This mechanism is known as the abstraction where only the essential characteristics of an object are shown while ignoring the other characteristics.

In Java, abstraction is achieved through the abstract classes and interfaces.

Abstract class in Java

The keyword “abstract” is used for abstract classes and methods in Java. The abstract class has the abstract keyword in its declaration. 

The syntax of a Java abstract class is:

abstract class class_name

    {

            public abstract void the_abstractMethod();

            public void the_normalMethod()

            {

                    #body of the method

            }   

    }   

The above syntax shows that both normal and abstract methods can be contained in an abstract class.

A few properties of the abstract classes are:

  • Abstract methods may or may not be present in the Java abstract class.
  • The presence of at least one abstract method in a class makes the class an abstract class.
  • An abstract class cannot have any objects and therefore cannot be directly instantiated.
  • An abstract class can be used only if it is inherited from another class and implements the abstract methods.
  • Proper implementations of the abstract methods are required while inheriting an abstract class.
  • Both regular and abstract methods can be present in a Java abstract class.
  • Parameterized constructors may be present in an abstract class. Also, an abstract class always contains a default constructor.

Abstract methods

Abstract methods are those types of methods that don’t require implementation for its declaration. These methods don’t have a body which means no implementation. A few properties of an abstract method are:

  • An abstract method in Java is declared through the keyword “abstract”.
  • While the declaration of the abstract method, the abstract keyword has to be placed before the name of the method.
  • There is no body in an abstract method, only the signature of the method is present. 
  • An abstract method in Java doesn’t have curly braces, but the end of the method will have a semicolon (;)

abstract double Method(double n1, double n2);

  • Any class that contains an abstract method should be declared as an abstract class. Although the opposite might not be true i.e. it is not necessary that an abstract class should have an abstract method.
  • Inheritance of an abstract class by a regular class requires the implementation of all the abstract methods in the parent class.

Example of an abstract method

public abstract class Car

{

    private String model;

  private String color;

    private int year;

    public abstract double computePrice();

}

If derived classes don’t implement the abstract methods, it would result in abstract classes that cannot be instantiated. 

A few cannot be used with the abstract keyword in Java.

  • final
  • abstract native
  • abstract static
  • abstract private
  • abstract synchronized
  • abstract strictfp

A code showing the use of an abstract method in Java is shown below. 

Source

The output of the code is:

Java Interfaces

Other than the abstract classes and abstract methods, Java uses interfaces to achieve abstraction. When related methods are grouped together into empty bodies, then it is called an interface. The interface is similar to an abstract class as it is the grouping of abstract methods. When a class implements the interface, all the abstract methods within the interface are inherited. The interface might also contain default methods, constants, and static methods. The method bodies of only the default and the static methods are present. An inheritance and a class can be written in a similar way but a class can describe an object’s attributes and behaviour. While in the case of an interface only those behaviours of an object are present, those are implemented by the class.

A class has to define all the methods of the interface that it implements. Only for an abstract class, the methods are not required to be defined by the class.

An interface is similar to a Java abstract class in the following ways:

  • Any number of methods can be contained in an interface.
  • .java extension is used for writing an interface in a file. The name of the file should match the name of the interface.
  • The .class file contains the byte code of an interface.
  • Interfaces appear in packages. The bytecode file of an interface is present in a directory with the same name as the package name.

An interface is different from a Java abstract class in the following ways:

  • The interface cannot be instantiated.
  • There are no constructors in an interface.
  • The interface contains only abstract methods.
  • No instance fields are present in an interface. A field has to be declared both as static and final if it has to appear in an interface.
  •  Only a class can implement an interface, but cannot be extended.
  • An interface can extend multiple interfaces.

Properties of an Interface

  • There is no need for declaring an interface as abstract through the abstract keyword as it is implicitly abstract.
  • Abstract keywords are not required to declare the methods as abstract as they are implicitly abstract.
  • All the methods in an interface are implicitly public.

How to declare an interface?

The keyword “interface” is used to declare an interface. Total abstraction is provided through the use of an interface. By default, all the methods within an interface are abstract and public. This means there is nobody of the methods in an interface and all the fields are static, public, and final. 

Syntax of an interface: 

Interface name_of_the_interface {

# declare fields

# declare abstract methods

}

For example interface Player

{

     final int age = 40;

    int calc();

}

How to implement an interface?

To implement an interface the keyword ‘implements’ is used. More than one interface can be implemented by a class. In such cases, the keyword ‘implements’ is followed by a list of interfaces separated by a comma. The keyword is used in the class declaration.

An Example of implementing the interface is shown below:

class Maruti implements Car

An example of a code showing the implementation of an interface in Java is shown below.

Source

The code generates the following output:

A code applying the implementation of multiple interfaces is shown below. 

Source 

The code generates the following output:

Some text…

Some other text…

Why interface is used?

  • Interfaces are used for achieving total abstraction.
  • Multiple inheritances in Java can be achieved through the use of inheritance as Java does not support multiple inheritances.
  • Loose coupling can be achieved through the use of an interface.
  • Through the use of implementation abstraction in java can be achieved.

Although abstraction in Java can be achieved through Java abstract class, interfaces are used as they contain final, public and static variables. However, non-final variables may be present in an abstract class.

If both the Java interface and Java abstract class are used to achieve abstraction, when should an interface and abstract class be used?

  • An abstract class is used when the user needs to define a template for a group of subclasses.
  • An interface is used when a user needs to define a role for other classes.

A few advantages of abstraction are:

  • The complexity of the program is reduced.
  • There are lesser chances of code duplication increasing the reusability.
  • The security of a program is increased through the use of abstraction as only a few details are provided to the user.

Conclusion

The article discussed the detailed concept of abstraction in Java which is a technique of hiding non-essential information from the user. In Java, abstraction is achieved through abstract classes and interfaces. A 100% abstraction is provided by the interfaces while a partial abstraction is provided by the abstract classes. The article further mentioned when to use an interface and when an abstract class should be used. It also listed out the similarities of an interface to that of Java abstract class and also the differences. Without the requisite knowledge of both the concepts will result in a code full of flaws.

If you are further interested in learning more and implementing the concepts in real-life problems, you can check out the Masters in Computer Science course provided by upGrad and IIIT-Bangalore recognized by Liverpool John Moores University. The course is designed for entry-level professionals (21-45 years of age) providing 500+ hours of training from leading industry experts and software engineering faculties. Further details of the course can be checked at upGrad’s official webpage. 

Upskill Yourself & Get Ready for The Future

12+ PROJECTS & ASSIGNMENTS, 1-ON-1 WITH INDUSTRY MENTORS, 500+ HOURS OF LEARNING
Apply Now

0 replies on “Abstract Class in Java and Methods [With Examples]”

×