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.
To begin with, let us try to understand what is an abstract method.
In terms of object-oriented programming, abstraction means that you hide a few unnecessary details of implementation from a user. This, in turn, leads to a better focus on the essential details or functionality. This also increases the overall efficiency and thus reduces complexity. Once you understand what is an abstract method, you can better understand what an abstract class is as well.
Check Out upGrad’s Java Bootcamp
Abstract methods in java mean those methods that stand declared with the usage of abstract keywords inside an abstract class. These do not have any definition per se and thus, are called abstract methods 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.
Check Out upGrad’s Advanced Certification in Cyber Security
When you have to work with any method declaration in super class, you can do so by the declaration of any of the abstract methods in java as an abstract one.
Abstract class in java example can be best defined as a subclass responsibility without any implementation in super class. This is why any subclass needs to override the same for providing a method definition. The best way to understand abstract class in java example is to understand that abstraction will be achieved with the use of abstract classes as well as methods. In this tutorial, we will learn about abstract methods and their use in Java.
What Abstract Class Contains?
A class stands declared as abstract on using an abstract keyword. This may have zero or more methods that are abstract or non-abstract. We must extend what an abstract class contains and then implement the methods. Instantiation isn’t possible.
Check out our free courses related to software development.
Explore Our Software Development Free Courses
In Java, abstraction is achieved through the abstract classes and interfaces.
upGrad’s Exclusive Software and Tech Webinar for you –
SAAS Business – What is So Different?
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.
Top Features Of Abstract Class
The top features of an abstract class are listed below-
- Code reusability
- Template
- Loose coupling
- Abstraction
- Dynamic resolution
There are five important rules you must follow when using abstract classes in Java. These are listed below-
- The keyword “abstract” must be used mandatorily when declaring an abstract class in Java.
- Abstract class cannot directly get instantiated.
- Abstract class needs to have at least one method that is abstract. An abstract class always contains final methods.
- Such classes might also comprise non-abstract methods.
- Abstract class might comprise constructors as well as static methods.
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.
In-Demand Software Development Skills
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.
Our learners also read: Free java course!
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.
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.
Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
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();
}
Explore our Popular Software Engineering Courses
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.
The code generates the following output:
A code applying the implementation of multiple interfaces is shown below.
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.
Read our Popular Articles related to Software Development
Why Learn to Code? How Learn to Code? | How to Install Specific Version of NPM Package? | Types of Inheritance in C++ What Should You Know? |
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.
What is Java?
Java is an object-oriented programming language, which implies that all programmes are made up of entities that represent concepts or physical objects, which are referred to as objects. Desktops, servers, portable devices, smart cards, and discs all include Java programmes. It creates software for a variety of platforms. When a programmer creates a Java application, the compiled code, also known as bytecode, is generated. They are compatible with a wide range of operating systems (OS), including Windows, Linux, and Mac OS. Java requires simply the installation of the Java Runtime Environment (JRE) to function, independent of the instrument used.
What is a Class in Java?
In Java, a class is a logical template for creating objects who have the same characteristics. Objects are the units or the group of things which together form the classes. The essential class attributes should be present in all class objects. The actual attributes or values, as well as any methods that the object may utilize, are considered core properties. There are several elements to a class declaration. Modifiers, Class name, Superclass (if available), Implemented Interfaces, Appropriate Keywords (depending on if the class expands from a Superclass or handles one or more interfaces), and Class body within curly brackets are all included.
Where do we use interfaces in Java?
In Java, an interface is a list of method prototypes. Users can design an interface if they need to direct the programmer or construct a contract stating how the methods and fields of a type should be. Users must implement this interface, supply the body for all of the interface's abstract methods, and retrieve the object of the implementing class to create an object of this kind. The interface's goal is to facilitate communication while also introducing multiple inheritance. Communication is one of the functions of the interface. Users can describe how they want a type's methods and fields to work through the interface. Multiple inheritance is not supported by Java; nevertheless, users can accomplish multiple inheritance by utilizing interfaces.
What is Java?
What is a Class in Java?
In Java, a class is a logical template for creating objects who have the same characteristics. Objects are the units or the group of things which together form the classes. The essential class attributes should be present in all class objects. The actual attributes or values, as well as any methods that the object may utilize, are considered core properties. There are several elements to a class declaration. Modifiers, Class name, Superclass (if available), Implemented Interfaces, Appropriate Keywords (depending on if the class expands from a Superclass or handles one or more interfaces), and Class body within curly brackets are all included.
Where do we use interfaces in Java?
In Java, an interface is a list of method prototypes. Users can design an interface if they need to direct the programmer or construct a contract stating how the methods and fields of a type should be. Users must implement this interface, supply the body for all of the interface's abstract methods, and retrieve the object of the implementing class to create an object of this kind. The interface's goal is to facilitate communication while also introducing multiple inheritance. Communication is one of the functions of the interface. Users can describe how they want a type's methods and fields to work through the interface. Multiple inheritance is not supported by Java; nevertheless, users can accomplish multiple inheritance by utilizing interfaces.
