BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

What is abstract Keyword in Java

By Chaitanya Singh | Filed Under: java

The abstract keyword is used for classes and methods in Java. It is not an access modifier, but it is used to achieve abstraction in Java.

abstract class: An abstract class does not allow you to create an object of it. To access the methods and variables of this class, you must inherit this class.

abstract method: An abstract method is a method without body, you can declare it inside abstract class only. The body is defined by the sub class using the method overriding feature.

Abstract class and method Example

In this example, we have an abstract class MyAbsClass and this class contains an abstract method msg(). Another class JavaExample extends this class, this class has to override the abstract method of parent class, else it has to be declared abstract as well.

// abstract class
abstract class MyAbsClass {
  public String webName = "BeginnersBook.com";
  public int webAge = 12;
  public abstract void msg(); // abstract method
}

// Subclass: Inheriting abstract class "MyAbsClass"
class JavaExample extends MyAbsClass {
  // Overriding the msg() method here and providing the body
  public void msg() {
    System.out.println("Happy Learning!");
  }
  public static void main(String args[]){
    JavaExample obj = new JavaExample();
    System.out.println("Website Name: " + obj.webName);
    System.out.println("Website age: " + obj.webAge);
    obj.msg();
  }
}

Output:

Website Name: BeginnersBook.com
Website age: 12
Happy Learning!

Points to remember:

  • An abstract method can only be declared inside abstract class.
  • A class extending abstract class must have to override the abstract method or it should be declared abstract.
  • You cannot create an object of abstract class.
  • An abstract method cannot be declared static, final or private.
❮ Java Keywords

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap