• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

why we need abstract class?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell me answer. I am java fresher.

 
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To provide special classes.
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lucy,

Lets say i want to create a contract for other class, but don't want itself to be created.(i will make it interface).
When i know concrete implementation for a class i would like it to be a normal class.

But at times, there will be situation when i know some of the implementation and want to impose some contract over it, so that it could only be instantiated after fulfilling those requirements(overriding). Then i need abstract class.
 
Marshal
Posts: 79938
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do not “need” abstract classes; you could work quite happily with interfaces and concrete classes. Indeed, some languages eschew interfaces, too.
As harshvardhan ojha said, they are intermediate between interfaces and concrete classes, intended for situations where some but not all of the implementation is already known. But remember you can have abstract classes which are fully implemented (no abstract methods).
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes in some cases the abstract class may or may not contain any abstract methods. For Ex. In the case of "HttpServlet" class. This class contains all implemented methods but still it is an abstract class.
There are the cases in which you don't want to implement an Interface because you have to define all the methods of the implemented interface. In such cases these abstract classes becomes useful as they already contain method implementations. So when you extend the abstract class you just have to override the method you need.
 
Campbell Ritchie
Marshal
Posts: 79938
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinay chaturvedi wrote:Yes in some cases the abstract class. . .

Good answer
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Rai wrote:Please tell me answer. I am java fresher.


Another thing that abstract classes are useful for is as "skeleton implementations" for an interface; a good example of which is AbstractList.

For example, the following class (filched from Effective Java) wraps an Array as a read-only List:which is pretty powerful stuff, when you consider all the things you can do with a List (for example: iterating items in reverse order).

HIH

Winston
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

meeta gaur wrote:To provide special classes.


???
 
Andy Jack
Ranch Hand
Posts: 257
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Rai wrote:Please tell me answer. I am java fresher.



Say we are making a computer game that depicts the earth and all the living and non-living things in it.

When two (or more) things are closely related you can combine all their common functions and properties inside an Abstract class and then derive from it. Example. Bird and Mammal are living beings, they can move(), eat(), breathe(), reproduce(), exist as dimorphs(male/female). We know all this from our knowledge of biology. So, we create an Abstract class Animal with abstract methods like move(), eat() etc and variables like sex = male or female (or transgender???). Then Bird and Mammal define methods in their unique way - bird eats with beak, mammal with mouth etc.
Tomorrow, if we discover a new animal in some jungle, its quite likely that it will be similar to bird and mammal, at least in some ways. To define that new animal, we will simply extend from Abstract animal class and add the add the extra features or modify animal methods accordingly.

Interfaces are used when two things can perform similar functions, but are not very closely related. Example bird and bat can fly. Even airplanes can fly. So, is a bat a bird or vice versa ? No, bats don't lay eggs and they breastfeed. So we make an interface canFly or flyable or something like that. Bat and
bird implement that interface. When a third person reads the documentation for bat and bird, he sees "implements canFly" and extends Animal. So, he knows immediately that they are animals, but not the same kind. They can fly (and have two feet etc) but a bat is not a bird.

(Hey, potato has skin. I have skin. So am i a potato ?...Monkey has hair, monkey has skin. It can walk, eat and reproduce like me. It looks like and shares a huge percentage of its DNA with me. So, am i a monkey..no i am like a monkey... )

I feel that these may not be perfect examples. But, i thought they could guide you and maybe you can think of a better example.

 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assume that scientists have not discovered Ostrich, Penguin, Chicken which cannot fly Chicken can fly for about 20 seconds When they discover that, they will have to change bird class. Flying bird, non flying bird. Flying implements canFLy and other one does not.
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic