why do we need abstract classes and interfaces in java and what is the use of abstract classes and interfaces

Abstract class in java definitioin

As you all know that abstract class is a one of the important concept in all object oriented programming.So java also have abstract class concept.So What is abstract class?Why do we use abstract class in our normal programming? and How to use abstract classes?.For all these questions this hub gives you the answers.

We can say abstract definition in two ways,

Way 1 - In General or Real time:

In general we can say that abstract class does not have any real time object.You can have question, if there is no object for class , then why do we need the abstract classes.I will give answer to this question by below example class hierarchy.See the below image.  

Abstract class real time example

Abstract Class Real time example
Abstract Class Real time example

Abstract class realtime example explanation

You can see the above image is for example for Abstract class in real time.Take three classes Human, Man and Woman.All Man and Woman belong to Human category.In Original object exists for only for Man or Woman.NO object exist for Human category.But both Man and Woman have common characteristics where we can say it as Human characters.Now you can understand that why do we need abstract class.


Difference between Abstract class and interface

Now As you know that abstract class where we can not create object for abstract class.Same like abstract class, you can not create object for interface also.Then what is the difference between abstract class and interfaces in java.I can say that interface is for "PURE ABSTRACTION".Pure abstraction means you can not have concrete methods.But an abstract class can have concrete methods.Interfaces should not have any concrete methods, it should only have method declarations.

Which one is advantage over another:

Interface has advantage over Abstract class.Since you can not use abstract class for already inherited class.But you can use interface for already inherited class.

Comments 51 comments

ainur 4 years ago

that is indeed great) thanks) it was very useful)


jayasri 4 years ago

thank u soooooooooo much:)


malli 4 years ago

ya something its fine


pinku 4 years ago

thanks ................


yogesh 4 years ago

It's very important. Thnx...


Gaurav Sarna 4 years ago

very nice sirr. plz send a best example gauravsarna38@gmail.com


BENNY 4 years ago

WONDERFUL IT WAS VERY HELPFUL

I WAS SEARCHING FOR IT ALL OVER NOW I GOT A CLEAR IDEA

THANK YOU SOOOOOOOOOOOOOOO MUCH :)


Lakshmi SK 4 years ago

Your Example and Explanation is good.. Really Helped...


Aby Paul 4 years ago

At-last i got it. thanks bosssssssssssssssssss

you done good work 4 lot of people.


Udhayakumar G K 4 years ago


mohamed azarudeen 4 years ago

well done sir plz send some abstract real time examples to my mail id my id is azarsof@gmail.com


Kavita 4 years ago

Awesome explanation..:) :)..it made my concepts clear..:)


Narayanarao 4 years ago

Yes it is very useful examle.

sir plz send some abstract real time examples to my mail id my id is narayana.vana3@gmail.com


prakash 4 years ago

Yes, It's really very nice deifnition


rajnik vaishnav 4 years ago

thanks !!!!!

Really excellent concept understanding............


v.s.muthu kumar 4 years ago

its very useful to learn abstract class


srinvas 3 years ago

This is still abstarction in case of interface


SUNIL YADAV 3 years ago

every ans is right.


prashant 3 years ago

Difference between Abstract class and interface is grt answer bt above ans is like inheritance ..........then why we use absract n interface if inheritance present in java


akanksha 3 years ago

thanks.............


rammy 3 years ago

thanks..........................


ask 3 years ago

thank you...............


Rohtash Jangra 3 years ago

@Parshant

I am agree with your comment diff is great but from above depicted image pure inheritance is using. what is use of abstract in above example.


vinodainapur 3 years ago

By seeing the diagram i clearly understood about abstract concept


Sandeep 3 years ago

the image you have posted is quite explanatory. I also wanted to share here that abstract classes can contain concrete methods which are used as it is by its sub classes. The code example can be seen on my blog at http://www.javaexperience.com/java-methods-of-an-a...


Satish Verma 3 years ago

We cant instantiate abstract classes in general but note it down, we can instantiate abstract class if it doesn't have abstract method..........but not in the case of interfaces.......


Satish Verma 3 years ago

We cant instantiate abstract classes in general but note it down, we can instantiate abstract class if it doesn't have abstract method..........but not in the case of interfaces.......

by using syntax:

abstract_classname ref_variable_name= new abstract_classname() {};


venky 3 years ago

Awesome explanation


kiran 3 years ago

if we only declare method in abstract class and give the implementation in the other class where we are extending abstract class,so what is the need of abstract class while we are just declaring the method over there???????


Talha Khan 3 years ago

Thankyew So Muchh..


arjun 2 years ago

Its really helpful, but i want to know if(an example to clarify my doubt) there are three classes an abstract class named Animals, second class named dog, a third class named cat. We are declaring a method named eat() in the abstract class. Like u said in the above given info we can use this eat() in both the classes named dog and cat. Can i ask u something?, isn't it same if we define this eat() in both the dog and cat classes without the help of an abstract class. well what is the difference or what is the use.


jay 2 years ago

very easily understand .. :)


Haneef 2 years ago

Excellent man....really impressive


Eashwari 2 years ago

wow super


vanithasri 2 years ago

Really nice example


hari kaspa 2 years ago

Now nly,, am clear about Abstract

tHnQ it's really helpful


Mahesh 2 years ago

Hats Off to you.. :)


vinod 2 years ago

explanation is very good but i have the same Q as the arjun asked 7 weaks ago...

He said...

" Its really helpful, but i want to know if(an example to clarify my doubt) there are three classes an abstract class named Animals, second class named dog, a third class named cat. We are declaring a method named eat() in the abstract class. Like u said in the above given info we can use this eat() in both the classes named dog and cat. Can i ask u something?, isn't it same if we define this eat() in both the dog and cat classes without the help of an abstract class. well what is the difference or what is the use. "


gokila 2 years ago

super..........


priya 2 years ago

thanks


Ramakrishna 2 years ago

So good sir ,I understood something.....can u please send me some example programs regarding above topic ....my mail id is

ramakrishna0804@gmail.com


seetharamaiah 2 years ago

i Have one doubt sir i want develop thread example

i have three threads t1={1,2,3,4,5},t2={6,7,8,9,10},t3={11,12,13,14 .

final out put is={1,6,11,2,7,12,3,8,13- - - -} please send example to my id sitram.yanamala@gmail.com


Ashok 2 years ago

Very nice please give example program also


Ranjita 2 years ago

I answered same but interviewer s not satisfied by my answer..:((


rathod 2 years ago

For beginners its really nice explanation.


Saurabh 2 years ago

i have not seen a better explanation of abstract classes


Hem Chandra Bhatt 2 years ago

ITS Really great post


Akash 2 years ago

perfect answer


sajan 2 years ago

Thanku......


Test 13 months ago

good


Test2 11 months ago

haaha

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working