1

With a abstract class if i do not create any abstract method() then what is the meaning of abstract class. What is the purpose of abstract class in an application or in that case what is difference between abstract class and normal class in java?

and how do we create a object of abstract class?

4 Answers 4

2

Abstract classes let you add base behavior so programmers don't have to code everything, while still forcing them to follow your design.

The main goal of an abstract class is to provide shared implemenentation of common behaviour - promoting the reuse of code. The abstract class can be used as base classes that can be extended by subclasses to work on actual implementation.

Below is a good example of abstract class usage:

public abstract AbstractFlow{ public void initialStep() { //implementation directly in abstract superclass } public abstract void implement(); // implemented by subclasses public void afterAction() { //implementation directly in abstract superclass } } 

It is a template for other classes using it.

If we can consider a live example:

Abtract Class Cricket 

All forms of Cricket including 20-20, Test, One Day are its concrete classes which have their base functions already implemented.

Also a class can be abstract without any methods being abstract in very rare cases.

Sign up to request clarification or add additional context in comments.

Comments

1

You cannot instantiate an abstract class, in other words, if you have...

abstract class MyAbstractClass() { ... }

...you cannot do the following...

MyAbstractClass object = new MyAbstractClass();

While you can get the same result with a private constructor, with an abstract class you can still have other classes extending your abstract class which then CAN (potentially) be instantiated.

The typical use for something like this is to have an abstract base class. For example, imagine you might want to store Data about Cars and Motorcycles. Then you might have a common base class Vehicle for both your classes Carand Motorcycle. Even if Vehicle does not have any abstract methods, you might not want anyone to create a pure "Vehicle" object, but only Car and Motorcycle objects. Thus you make your Vehicle class abstract and get exactly that.

Comments

1

With a Abstract class if i do not create any abstract method() then what is the meaning of abstract class.

By definitions from official docs

An abstract class is a class that is declared abstract—it may or may not include abstract methods.

Then in which purpose abstract class will use in application or in that case what is difference between abstract class and normal class in java?

Abstract classes cannot be instantiated, but they can be subclassed.

Comments

1

An abstract class is super useful to define common methods and attributes to a set of classes. I'll try and give an example:

Imagine you are designing an application for managing staff or whatever at a certain company. You will have different kinds of users:

  • employees
  • guests
  • managers
  • directors

Even though they have different characteristics, there are many common attributes such as name, id, age and so on. Following this thread of thought, we define a super class User:

public class User { public String name; public int age; public String id; public User(String name, int age, String id) { this.name = name; this.age = age; this.id = id; } //here would be the getters and setters } 

Now, the other classes would be declared as extensions of this User class (check java heritance out).

public class Employee extends User{//define specific methods} public class Manager extends User{//as above} //and so on... 

Now, although you want all these subtypes of user to inherit commons and attributes, you don't want to create Users per se, you don't want anyone to be of type User, only subtypes of the class. Therefore, you don't want User to be instantiated and that's why the keyword abstract is used.

By changing the User class declaration to abstract, you are telling that you don't want this class to be instantiated and prevent having users of type User, guaranteeing the only users you'll have created are Employee, Director, etc:

public abstract class User {} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.