Show A class which has the abstract keyword in its declaration is called abstract class. Abstract classes should have zero or more abstract methods. i.e., methods without a body. It can have multiple concrete methods. Abstract classes allow you to create blueprints for concrete classes. But the inheriting class should implement the abstract method. Abstract classes cannot be instantiated. Important Reasons For Using Interfaces
Important Reasons For Using Abstract Class
The interface is a blueprint that can be used to implement a class. The interface does not contain any concrete methods (methods that have code). All the methods of an interface are abstract methods. An interface cannot be instantiated. However, classes that implement interfaces can be instantiated. Interfaces never contain instance variables but, they can contain public static final variables (i.e., constant class variables) Difference between Interface and Abstract Class in JavaAn abstract class permits you to make functionality that subclasses can implement or override whereas an interface only permits you to state functionality but not to implement it. A class can extend only one abstract class while a class can implement multiple interfaces.
Sample code for Interface and Abstract Class in JavaFollowing is sample code to create an interface and abstract class in Java Interface Syntax interface name{ //methods }Java Interface Example: interface Pet { public void test(); } class Dog implements Pet { public void test() { System.out.println("Interface Method Implemented"); } public static void main(String args[]) { Pet p = new Dog(); p.test(); } }Abstract Class Syntax Abstract class example: abstract class Shape { int b = 20; abstract public void calculateArea(); } public class Rectangle extends Shape { public static void main(String args[]) { Rectangle obj = new Rectangle(); obj.b = 200; obj.calculateArea(); } public void calculateArea() { System.out.println("Area is " + (b * b)); } }Abstract class in Java is similar to interface except that it can contain default method implementation. An abstract class can have an abstract method without body and it can have methods with implementation also. abstract keyword is used to create a abstract class and method. Abstract class in java can’t be instantiated. An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class. Here is a simple example of an Abstract Class in Java. package com.journaldev.design; //abstract class public abstract class Person { private String name; private String gender; public Person(String nm, String gen){ this.name=nm; this.gender=gen; } //abstract method public abstract void work(); @Override public String toString(){ return "Name="+this.name+"::Gender="+this.gender; } public void changeName(String newName) { this.name = newName; } }Notice that work() is an abstract method and it has no-body. Here is a concrete class example extending an abstract class in java. Note that subclass Employee inherits the properties and methods of superclass Person using inheritance in java. Also notice the use of Override annotation in Employee class. Read more for why we should always use Override annotation when overriding a method. Abstract class in Java Important Points
That’s all for an abstract class in Java. If I missed anything important, please let us know through comments. |