topics content@scaler.com
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: Abstract Class in Java - Scaler Topics description: Learn about abstract class in Java by Scaler Topics. This article will help you understand all the concepts of Abstract Classes in Java. Read to learn more. author: Shikhar Baniya category: Java --- :::section{.abstract} You might wonder, "What is an abstract class in Java?" An abstract class cannot be instantiated and is primarily meant to be subclassed by other classes. A class that is declared with the keyword having `abstract` is known as an abstract class in Java. It may have abstract and non-abstract methods. An abstract method is an method that is declared without any implementation. Abstract classes in Java implement the concept of [Abstraction](https://www.scaler.com/topics/java/abstraction-in-java/) in Object Oriented Programming. ::: :::section{.main} ## What is Abstract Class in Java? An abstract is a small description that gives you an imaginative idea of the concept. Like a summary, a small paragraph gives you a brief explanation of the whole book. In the same way, **Abstract Classes** in Java simply declares the methods that need to be implemented by the class, which extends the abstract class. The abstract class hides the internal details of method implementation, showing only essential information to the user, such as the method name. *Hiding internal details and showing only the functionality is known as abstraction.* Abstraction is one of the key concepts in the object-oriented programming paradigm. **To implement Abstraction, we use Abstract Classes or Interfaces**. Abstract class in Java is a collection of abstract and non-abstract methods. Abstract methods do not have a body or implementation. The child classes, which extend the abstract class, provide the implementation of the abstract methods. An Abstract Class is nothing but a blueprint for the child class. *Abstract classes justify the layout of your idea and do not really implement them.* **Syntax:** ```java abstract class ClassName { // Abstract and Non-abstract methods } ``` ### Illustration ```java abstract class Circle { int height; // An abstract function abstract void area(); } ``` ## Some Important Points about Abstract Classes Let's recap the rules to declare an abstract class - * You have to use the keyword `abstract`. * You cannot instantiate an abstract class. * An abstract class can contain both abstract and non-abstract methods. * You can include non-abstract final methods (a method that cannot be overridden) in your abstract class as well. * Final methods in abstract classes can not be abstract. They must be implemented in the abstract class itself. * You can also include constructors and non-abstract static methods in your abstract class. ::: :::section{.tip} **Must Read- [When do We Use an Abstract Class in Java?](https://www.scaler.com/topics/when-do-we-use-an-abstract-class-in-java/)** ::: :::section{.main} ## Abstract Method in Java Using the **abstract** keyword while declaring the method is called an Abstract Method. An [Abstract Method](https://www.scaler.com/topics/abstract-method-in-java/) does not contain anybody. Whenever you declare an abstract method, you must end it with a semicolon, as shown in the syntax below. An abstract method is always present inside an abstract class; you cannot declare it inside the regular (not abstract) class. Let's understand some of the rules on the usage of the Abstract Method - * A method declared with the abstract keyword is called an Abstract method. * Abstract method can only be declared inside an abstract class or an interface. * Abstract methods must not contain any definition or body in abstract class. * You must end the declaration of the abstract method using ';'(semicolon). * To write the implementation code of these abstract methods, you must inherit the abstract class. Then, the child class can define the abstract methods. * If you don't define the abstract method inside the child class, then you must declare the child class as abstract; otherwise, the compiler will throw an error. This is illustrated below: ```java abstract class Calc { abstract void display(); } class Computer extends Calc{ } public class Main { public static void main(String[] args) { System.out.println("Hello World"); } } ``` Output: ```java Main.java:17: error: A is not abstract and does not override abstract method display() in Calc class A extends Calc{ ^ 1 error ``` ### Syntax of Abstract Method The syntax for the abstract method is similar to any user-defined method in Java, except you have to add the **abstract** keyword at the start of the declaration. For example - ```java abstract return_type method_name(parameters list); ``` ### Java Abstract Class Examples Let's understand abstract class and methods in depth using a Java example. ### Example of Abstract Class That Has Abstract Method **See the following example -** ```java abstract class Calculator { abstract void display(); } class Add extends Calculator { void display() { System.out.println("In the Add Child Class"); } } class Sub extends Calculator { void display() { System.out.println("In the Sub Child Class"); } } class AbstractMethodExample { public static void main(String arg[]) { Calculator add = new Add(); add.display(); Calculator sub = new Sub(); sub.display(); } } ``` **Output:** ```java In the Add Child Class In the Sub Child Class ``` **Explanation:** * We have created two classes - `Add` and `Sub` which extend the parent abstract class `Calculator`. * Since `Add` and `Sub` inherit the abstract class, they must implement the abstract method `display()`; otherwise, an error will be thrown. * Next, we declare a class **'Main'**. In the main method, using the `Calculator` abstract class, we declare two objects, `add` and `sub`. Now, using these objects, we can access the methods inside the child class and hence the output. ### Abstract Class Having Constructor, Data Member, and Methods ```java import java.io.*; abstract class Animal { String name; Animal(String name) { this.name = name; System.out.println("A new " + name + " is created"); } abstract void sound(); void eat() { System.out.println(name + " is eating"); } } class Dog extends Animal { Dog() { super("Dog"); } void sound() { System.out.println("Woof"); } } class Cat extends Animal { Cat() { super("Cat"); } void sound() { System.out.println("Meow"); } } class Main { public static void main(String[] args) { Animal pet1 = new Dog(); Animal pet2 = new Cat(); pet1.sound(); pet1.eat(); pet2.sound(); pet2.eat(); } } ``` **Output:** ``` A new Dog is created A new Cat is created Woof Dog is eating Meow Cat is eating ``` **Explanation:** * When a new Dog() is called, it invokes the constructor of the Dog class, which calls the constructor of the Animal class with the name "Dog". This prints "A new Dog is created". * Similarly, when a new Cat() is called, it invokes the constructor of the Cat class, which in turn calls the constructor of the Animal class with the name "Cat". This prints "A new Cat is created". * pet1.sound() invokes the sound() method of the Dog class, printing "Woof". * pet1.eat() invokes the eat() method of the Animal class with the name "Dog", printing "Dog is eating". * pet2.sound() invokes the sound() method of the Cat class, printing "Meow". * pet2.eat() invokes the eat() method of the Animal class with the name "Cat", printing "Cat is eating". ::: :::section{.main} ## Properties of Java Abstract Class * Use the **'abstract'** keyword to declare a class or method abstract. * You have to implement all the abstract methods declared within the abstract class in its subclasses. * The class has to be abstract if you want to declare an abstract method within its scope. * You cannot create objects of an abstract class because the abstract class cannot be instantiated. * Whenever you create an object of the child class of an abstract class, the compiler calls the constructor of the abstract class automatically. * You can declare non-abstract final and static methods inside abstract class in java. * Multiple inheritances cannot be achieved using an abstract class. ::: :::section{.summary} ## Conclusion * Abstraction is one of the key principles for implementing the Object-Oriented Programming Approach and design in Java. * When you use the ‘abstract’ keyword while declaring a method, it is called an abstract method. * Abstract methods are implemented in the subclasses of the abstract class within which they are declared. * Abstract class in Java can partially implement one or more interfaces. * We can declare non-abstract static and final methods in Java. We must provide their implementation in the abstract class itself. * We cannot create instances of abstract classes because they may contain non-implemented methods that have no operations to perform. ::: :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully