0% found this document useful (0 votes)
4 views7 pages

Abstract Class in Java With Example

An abstract class in Java is declared using the 'abstract' keyword and can contain both abstract methods (without body) and concrete methods (with body). It cannot be instantiated directly, and any subclass must implement all abstract methods or be declared abstract itself. Abstract classes provide a way to enforce a contract for subclasses while allowing for shared functionality through concrete methods.

Uploaded by

Anima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Abstract Class in Java With Example

An abstract class in Java is declared using the 'abstract' keyword and can contain both abstract methods (without body) and concrete methods (with body). It cannot be instantiated directly, and any subclass must implement all abstract methods or be declared abstract itself. Abstract classes provide a way to enforce a contract for subclasses while allowing for shared functionality through concrete methods.

Uploaded by

Anima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Abstract Class in Java with example

By Chaitanya Singh | Filed Under: OOPs Concept

A class that is declared using “abstract” keyword is known as abstract class. It can have abstract
methods(methods without body) as well as concrete methods (regular methods with body). A normal
class(non-abstract class) cannot have abstract methods. In this guide we will learn what is a abstract
class, why we use it and what are the rules that we must remember while working with it in Java.

An abstract class can not be instantiated, which means you are not allowed to create an object
of it. Why? We will discuss that later in this guide.

Why we need an abstract class?

Lets say we have a class Animalthat has a method sound()and the subclasses(see inheritance) of
it like Dog, Lion, Horse, Catetc. Since the animal sound differs from one animal to another, there is
no point to implement this method in parent class. This is because every child class must override this
method to give its own implementation details, like Lionclass will say “Roar” in this method and Dog
class will say “Woof”.

So when we know that all the animal child classes will and should override this method, then there is
no point to implement this method in parent class. Thus, making this method abstract would be the
good choice as by making this method abstract we force all the sub classes to implement this method(
otherwise you will get compilation error), also we need not to give any implementation to this method
in parent class.

Since the Animalclass has an abstract method, you must need to declare this class abstract.

Now each animal must have a sound, by making this method abstract we made it compulsory to the
child class to give implementation details to this method. This way we ensures that every animal has a
sound.

Abstract class Example

//abstract parent class


abstract class Animal{
//abstract method
public abstract void sound();
}
//Dog class extends Animal class
public class Dog extends Animal{
public void sound(){
System.out.println("Woof");
}
public static void main(String args[]){
Animal obj = new Dog();
obj.sound();
}
}

Output:

Woof

Hence for such kind of scenarios we generally declare the class as abstract and later concrete
classes extend these classes and override the methods accordingly and can have their own methods
as well.

Abstract class declaration

An abstract class outlines the methods but not necessarily implements all the methods.

//Declaration using abstract keyword


abstract class A{
//This is abstract method
abstract void myMethod();

//This is concrete method with body


void anotherMethod(){
//Does something
}
}

Rules

Note 1: As we seen in the above example, there are cases when it is difficult or often unnecessary to
implement all the methods in parent class. In these cases, we can declare the parent class as
abstract, which makes it a special class which is not complete on its own.

A class derived from the abstract class must implement all those methods that are declared as
abstract in the parent class.

Note 2: Abstract class cannot be instantiated which means you cannot create the object of it. To use
this class, you need to create another class that extends this this class and provides the
implementation of abstract methods, then you can use the object of that child class to call non-
abstract methods of parent class as well as implemented methods(those that were abstract in parent
but implemented in child class).

Note 3: If a child does not implement all the abstract methods of abstract parent class, then the child
class must need to be declared abstract as well.

Do you know? Since abstract class allows concrete methods as well, it does not provide 100%
abstraction. You can say that it provides partial abstraction. Abstraction is a process where you
show only “relevant” data and “hide” unnecessary details of an object from the user.

Interfaces on the other hand are used for 100% abstraction (See more about abstraction here).
You may also want to read this: Difference between abstract class and Interface in Java

Why can’t we create the object of an abstract class?

Because these classes are incomplete, they have abstract methods that have no body so if java
allows you to create object of this class then if someone calls the abstract method using that object
then What would happen?There would be no actual implementation of the method to invoke.
Also because an object is concrete. An abstract class is like a template, so you have to extend it and
build on it before you can use it.

Example to demonstrate that object creation of abstract class is not allowed

As discussed above, we cannot instantiate an abstract class. This program throws a compilation error.

abstract class AbstractDemo{


public void myMethod(){
System.out.println("Hello");
}
abstract public void anotherMethod();
}
public class Demo extends AbstractDemo{

public void anotherMethod() {


System.out.print("Abstract method");
}
public static void main(String args[])
{
//error: You can't create object of it
AbstractDemo obj = new AbstractDemo();
obj.anotherMethod();
}
}
Output:

Unresolved compilation problem: Cannot instantiate the type AbstractDemo

Note: The class that extends the abstract class, have to implement all the abstract methods of it, else
you have to declare that class abstract as well.

Abstract class vs Concrete class

A class which is not abstract is referred as Concrete class. In the above example that we have seen
in the beginning of this guide, Animalis a abstract class and Cat, Dog& Lionare concrete classes.

Key Points:
1. An abstract class has no use until unless it is extended by some other class.
2. If you declare an abstract method in a class then you must declare the class abstract as well. you
can’t have abstract method in a concrete class. It’s vice versa is not always true: If a class is not
having any abstract method then also it can be marked as abstract.
3. It can have non-abstract method (concrete) as well.

I have covered the rules and examples of abstract methods in a separate tutorial, You can find the
guide here: Abstract method in Java
For now lets just see some basics and example of abstract method.
1) Abstract method has no body.
2) Always end the declaration with a semicolon(;).
3) It must be overridden. An abstract class must be extended and in a same way abstract method
must be overridden.
4) A class has to be declared abstract to have abstract methods.

Note: The class which is extending abstract class must override all the abstract methods.

Example of Abstract class and method

abstract class MyClass{


public void disp(){
System.out.println("Concrete method of parent class");
}
abstract public void disp2();
}

class Demo extends MyClass{


/* Must Override this method while extending
* MyClas
*/
public void disp2()
{
System.out.println("overriding abstract method");
}
public static void main(String args[]){
Demo obj = new Demo();
obj.disp2();
}
}

Output:

overriding abstract method

By Chaitanya Singh | Filed Under: OOPs Concept

A method without body (no implementation) is known as abstract method. A method must always be
declared in an abstract class, or in other words you can say that if a class has an abstract method, it
should be declared abstract as well. In the last tutorial we discussed Abstract class, if you have not
yet checked it out read it here: Abstract class in Java, before reading this guide.
This is how an abstract method looks in java:

public abstract int myMethod(int n1, int n2);

As you see this has no body.

Rules of Abstract Method

1. Abstract methods don’t have body, they just have method signature as shown above.
2. If a class has an abstract method it should be declared abstract, the vice versa is not true, which
means an abstract class doesn’t need to have an abstract method compulsory.
3. If a regular class extends an abstract class, then the class must have to implement all the abstract
methods of abstract parent class or it has to be declared abstract as well.

Example 1: abstract method in an abstract class

//abstract class
abstract class Sum{
/* These two are abstract methods, the child class
* must implement these methods
*/
public abstract int sumOfTwo(int n1, int n2);
public abstract int sumOfThree(int n1, int n2, int n3);

//Regular method
public void disp(){
System.out.println("Method of class Sum");
}
}
//Regular class extends abstract class
class Demo extends Sum{

/* If I don't provide the implementation of these two methods, the


* program will throw compilation error.
*/
public int sumOfTwo(int num1, int num2){
return num1+num2;
}
public int sumOfThree(int num1, int num2, int num3){
return num1+num2+num3;
}
public static void main(String args[]){
Sum obj = new Demo();
System.out.println(obj.sumOfTwo(3, 7));
System.out.println(obj.sumOfThree(4, 3, 19));
obj.disp();
}
}

Output:

10
26
Method of class Sum

Example 2: abstract method in interface

All the methods of an interface are public abstract by default. You cannot have concrete (regular
methods with body) methods in an interface.

//Interface
interface Multiply{
//abstract methods
public abstract int multiplyTwo(int n1, int n2);
/* We need not to mention public and abstract in interface
* as all the methods in interface are
* public and abstract by default so the compiler will
* treat this as
* public abstract multiplyThree(int n1, int n2, int n3);
*/
int multiplyThree(int n1, int n2, int n3);

/* Regular (or concrete) methods are not allowed in an interface


* so if I uncomment this method, you will get compilation error
* public void disp(){
* System.out.println("I will give error if u uncomment me");
*}
*/
}

class Demo implements Multiply{


public int multiplyTwo(int num1, int num2){
return num1*num2;
}
public int multiplyThree(int num1, int num2, int num3){
return num1*num2*num3;
}
public static void main(String args[]){
Multiply obj = new Demo();
System.out.println(obj.multiplyTwo(3, 7));
System.out.println(obj.multiplyThree(1, 9, 0));
}
}

Output:

21
0

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy