Why abstract class used in java ?

hare singh nayak

hare singh nayak

@hare-singh-nayak-WEmcz0 Oct 26, 2024
hay friends

as we know that we can not create the instance of abstract class than why we use abstract class....

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Jun 6, 2013

    suppose you have to write code for TwoWheeler.
    Lets make a abstract class for TwoWheeler do following things in common
    1. Turning mechanism
    2. break mechanism
    But following things can be differ from kind of TwoWheeler
    A. Gear Mechanism

    you can write with implementation of 1 & 2 method in abstract class and provide a abstract method for GearMechanism.
    Now whatever class will extend TwoWheeler class, they already have method 1,2 and just need to implement method A. this give you proper reuse of code.

    PS: In java, always think as if in real scenarios, makes java like a cake. more you eat more you get amused😀
  • hare singh nayak

    hare singh nayak

    @hare-singh-nayak-WEmcz0 Jun 6, 2013

    ianoop
    suppose you have to write code for TwoWheeler.
    Lets make a abstract class for TwoWheeler do following things in common
    1. Turning mechanism
    2. break mechanism
    But following things can be differ from kind of TwoWheeler
    A. Gear Mechanism

    you can write with implementation of 1 & 2 method in abstract class and provide a abstract method for GearMechanism.
    Now whatever class will extend TwoWheeler class, they already have method 1,2 and just need to implement method A. this give you proper reuse of code.

    PS: In java, always think as if in real scenarios, makes java like a cake. more you eat more you get amused😀

    Thank you anoop

    you have explain in very nice way and any one can easily understand...