• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstraction  RSS feed

 
ssiva raman
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

in abstract class,we write some methods abstract and some with implementation.right?
so my question here is,what kinds of methods should be made abstract and with implementation in a abstract class.

help me with this question.....
 
mostafa jamareh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods. Let's look at an example of an abstract class, and an abstract method.

Suppose we were modeling the behavior of animals, by creating a class hierachy that started with a base class called Animal. Animals are capable of doing different things like flying, digging and walking, but there are some common operations as well like eating and sleeping. Some common operations are performed by all animals, but in a different way as well. When an operation is performed in a different way, it is a good candidate for an abstract method (forcing subclasses to provide a custom implementation). Let's look at a very primitive Animal base class, which defines an abstract method for making a sound (such as a dog barking, a cow mooing, or a pig oinking).



Note that the abstract keyword is used to denote both an abstract method, and an abstract class. Now, any animal that wants to be instantiated (like a dog or cow) must implement the makeNoise method - otherwise it is impossible to create an instance of that class. Let's look at a Dog and Cow subclass that extends the Animal class.



Now you may be wondering why not declare an abstract class as an interface, and have the Dog and Cow implement the interface. Sure you could - but you'd also need to implement the eat and sleep methods. By using abstract classes, you can inherit the implementation of other (non-abstract) methods. You can't do that with interfaces - an interface cannot provide any method implementations.

 
ssiva raman
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you explanation is helpful thankyou

from your's, i understood that a method in a abstract class, should be made abstract when a particular behaviour is common for a group of object but that behaviour is done specifically(overriden) by each object of that group like makesound() though it is common for all animals each animal(dog,cat.) do it differently.

secondly,a method in a abstract class is given implementation only if there is no need for the subclass objects to override those methods.

did i understand it right?
 
mostafa jamareh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you extends your abstract class you have to override the abstract methods of abstract class but you don't need to override non-abstract methods of abstract class
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please don't confuse the words. Abstract classes have nothing specific to do with abstraction. They simply have similar names.
You can have subclasses of abstract classes with abstract methods, as long as those subclasses are also marked abstract.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ssiva raman wrote:secondly,a method in a abstract class is given implementation only if there is no need for the subclass objects to override those methods.

Usually; but not always. Indeed, there is no requirement for an abstract class to contain ANY abstract methods. It's quite common, for example, to write a non-final method with a default implementation that just throws UnsupportedOperationException (I have a feeling that AbstractList may be just such an example, but I'm not absolutely sure).

The inverse is not true though: if a class contains any abstract methods then it MUST be abstract itself.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!