here is one program which has 1 abstract class without abstract method. its also works fine.
Then what the use of abstract class, any how it does not allow to create objects, we can use this with concrete class to implement method defined in abstract class which we have to extend the abstract class then why not directly in concrete class why need of abstract class.
can anyone explain this point.

PROGRAM

abstract class demo {
public void show() {
System.out.println("not abstract method");
}
// public abstract void display();
}
class demo1 extends demo {
public void display() {
System.out.println("abstract method");
}}

class program {
public static void main(String args[]) {
demo d = new demo1();
d.show();
d.display();
//d.display1();
}
}

Recommended Answers

All 3 Replies

Abstract class have similiar functions as Interface. They're useful in inheritance and polymorphism. Read the java tutorial about Abstract Class for more info.

well, abstract classes are "abstract" and provides "abstraction". That's one way to look at it. Think of it like this:

They don't allow you to create objects, right? Now, why do you think that is required?
Lets take an example, say we have a Human class which has two subclasses, Male and Female.
Now, we can have male objects as well as female objects but what about human objects? Do we know how a human looks? have you ever seen a human separately?
So obviously it makes no sense to create human objects. So we mark the Human class as abstract and prevent it from being instantiated.

Obviously there are other uses to it which includes providing some default behaviors for all the subclasses that extends the abstract class. Google is always your friend.

first of all you should understand polymorphism,

that is inheritance which is main advantages of java the term "reusability".

in terms of inheritance java provide interface and abstract.

in an interface for example
class A contains method1 and method2.

class B contains some methods itself like method3 and method4.

in such case class B needs method1 and method2 from class A.
here we don't rewrite method1 and method 2 in class B instead of just
implements class A.

but the disadvantage of interface is , suppose class B just need method 2 only from class A but according to interface sub class (class B) should implement all the super class (class A) methods .
so for just need of method1 class B unnecessarily implements method 2 also.

in this situation abstract comes to play.
using as abstract , class B no need to define unnecessary methods from Class A.

conclusion:

if subclass has some difference from superclass but have some common behavior then use abstract.

if subclass exactly needs all the behavior from superclass then use interface.

disadvantages of abstract is we can't extend more then one abstract class.

but interface can implements more then one.

the choice is yours how to use interface and abstract as per your requirements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.