1. Home >
  2. Apps >
  3. Groups >

Why is abstract class used in java?

Question asked by hare singh nayak in #Java on Jun 5, 2013
hare singh nayak
hare singh nayak 路 Jun 5, 2013
Rank D2 - MASTER

Hey friends, as we know that we can not create the instance of abstract class than why we use abstract class?

Posted in: #Java
Anoop Kumar
Anoop Kumar 路 Jun 6, 2013
Rank A2 - PRO
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 路 Jun 6, 2013
Rank D2 - MASTER
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...
Mohana Chandra
Mohana Chandra 路 Jan 23, 2020
Rank C1 - EXPERT

Yes, Abstract classes cannot be instantiated. But by inheriting that class,means by creating sub class to the abstract class , we can access the abstract class. This can be done by creating object to the subclass,which will automatically execute the abstract class,because of inheritance.

If the abstract class has constructors,those can be accessed with Explicit Constructor chaining using the function "super()" ,which calls the super-class constructor.

Durgesh Srivastava
Durgesh Srivastava 路 Jan 23, 2020
Rank C2 - EXPERT

Abstract class in Java is similar to interface except that it can contain default method implementation. An abstract class can have an abstract method without body and it can have methods with implementation also.聽

Java Abstract class can implement interfaces without even providing the implementation of interface methods.

Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation.

We can run abstract class in java like any other class if it has main() method.

Chebet Sarah
Chebet Sarah 路 Jan 23, 2020
Rank D1 - MASTER

Java Abstract class can execute interfaces without giving the usage of interface strategies. Java Abstract class is utilized to give normal method usage to every one of the subclasses or to give default execution. We can run the conceptual class in java like some other class in the event that it has main() method.

You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up