• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

Abstract Class

As the name suggest, an Abstract class is a conceptual idea of an object but it doesn’t make sense to create objects from it as it may have methods that are not yet implemented.

When you know what an object does, but if the implementation can only be decided in the subclass then we declare the class and those unclear methods as abstract.

So, we can conclude that an abstract class is class with abstract methods. And, an abstract method is an empty method without implementation.

package com.techstackjournal.shapes;

public abstract class Shape {
	public abstract void draw();
}

A Shape is a non-representational object, it doesn’t have any form, so it would be correct to declare it as an abstract class.

Similarly, in Shape class we cannot write code to draw as we don’t know the shape of it. Each subclass of Shape, like Circle, Rectangle may draw the shape differently, so, it is correct to declare the draw method also as abstract within Shape class.

Also, please note that we need to declare the abstract method without body.

To declare a class as abstract, we do not need the methods to be abstract, we can force developers to inherit it the .

package com.techstackjournal.shapes;

public class Circle extends Shape {
	public void draw() {
		System.out.println("Drawing Circle");
	}
}

If a class extends an abstract class it must provide the implementation of all the abstract methods of the super class, else the subclass also needs to be marked as abstract class.

If a subclass implements all abstract methods, we call it as concrete class as it provides concrete implementation to the abstract methods.

In the above example, Circle class extends Shape class and provides implementation to draw method, so Circle class is a concrete subclass.

Primary Sidebar

More to See

Arrays.asList in Java Examples

February 21, 2021 By Admin

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 By Admin

Secondary Sidebar

  • Java Tutorial
    • Introduction
    • Install Java
    • First Java Program
    • Statements and Comments
    • Packaging the Classes
    • Variables
    • Primitive Data Types
    • Operators
    • if-else statement
    • Loops in Java
    • Arrays
    • break and continue
    • Switch Statement
    • Classes and Objects
    • Methods
    • Encapsulation
    • Constructor in Java: All You Need To Know
    • Inheritance
    • Access Modifiers
    • Static Variables and Methods
    • Final keyword in Java
    • Abstract Class
    • What is an Interface in Java?
    • Method Overloading
    • Java – Method Overriding
    • Polymorphism
    • Java – super Keyword
    • Nested Classes
    • Exception Handling
    • String Class

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Misc
  • Node.js
  • Spring Core
  • Windows

Archives

  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc

Recent

  • How to Make File Explorer Open to This PC instead of Quick Access in Windows 10
  • Arrays.asList in Java Examples
  • [Solved] Why List.add throws UnsupportedOperationException in Java?
  • How to Convert an Array to List in Java?
  • How Many Spaces in a Tab?

Search

Copyright © 2021 · Tech Stack Journal · Log in