ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

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

Updated on August 06, 2010

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

    0 of 8192 characters used
    Post Comment

    • profile image

      ainur 4 years ago

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

    • profile image

      jayasri 4 years ago

      thank u soooooooooo much:)

    • profile image

      malli 4 years ago

      ya something its fine

    • profile image

      pinku 4 years ago

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

    • profile image

      yogesh 4 years ago

      It's very important. Thnx...

    • profile image

      Gaurav Sarna 4 years ago

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

    • profile image

      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 :)

    • profile image

      Lakshmi SK 4 years ago

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

    • profile image

      Aby Paul 4 years ago

      At-last i got it. thanks bosssssssssssssssssss

      you done good work 4 lot of people.

    • profile image

      Udhayakumar G K 4 years ago

    • profile image

      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

    • profile image

      Kavita 4 years ago

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

    • profile image

      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

    • profile image

      prakash 4 years ago

      Yes, It's really very nice deifnition

    • profile image

      rajnik vaishnav 4 years ago

      thanks !!!!!

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

    • profile image

      v.s.muthu kumar 4 years ago

      its very useful to learn abstract class

    • profile image

      srinvas 4 years ago

      This is still abstarction in case of interface

    • profile image

      SUNIL YADAV 4 years ago

      every ans is right.

    • profile image

      prashant 4 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

    • profile image

      akanksha 4 years ago

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

    • profile image

      rammy 4 years ago

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

    • profile image

      ask 4 years ago

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

    • profile image

      Rohtash Jangra 4 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.

    • profile image

      vinodainapur 4 years ago

      By seeing the diagram i clearly understood about abstract concept

    • profile image

      Sandeep 4 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...

    • profile image

      Satish Verma 4 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.......

    • profile image

      Satish Verma 4 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() {};

    • profile image

      venky 4 years ago

      Awesome explanation

    • profile image

      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???????

    • profile image

      Talha Khan 3 years ago

      Thankyew So Muchh..

    • profile image

      arjun 3 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.

    • profile image

      jay 3 years ago

      very easily understand .. :)

    • profile image

      Haneef 3 years ago

      Excellent man....really impressive

    • profile image

      Eashwari 3 years ago

      wow super

    • profile image

      vanithasri 3 years ago

      Really nice example

    • profile image

      hari kaspa 3 years ago

      Now nly,, am clear about Abstract

      tHnQ it's really helpful

    • profile image

      Mahesh 3 years ago

      Hats Off to you.. :)

    • profile image

      vinod 3 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. "

    • profile image

      gokila 3 years ago

      super..........

    • profile image

      priya 3 years ago

      thanks

    • profile image

      Ramakrishna 3 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

    • profile image

      seetharamaiah 3 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

    • profile image

      Ashok 2 years ago

      Very nice please give example program also

    • profile image

      Ranjita 2 years ago

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

    • profile image

      rathod 2 years ago

      For beginners its really nice explanation.

    • profile image

      Saurabh 2 years ago

      i have not seen a better explanation of abstract classes

    • profile image

      Hem Chandra Bhatt 2 years ago

      ITS Really great post

    • profile image

      Akash 2 years ago

      perfect answer

    • profile image

      sajan 2 years ago

      Thanku......

    • profile image

      Test 19 months ago

      good

    • profile image

      Test2 17 months ago

      haaha

    Click to Rate This Article