Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstract definition & Interface  RSS feed

 
Shafian Kisna
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I need your help in the following definition. I am uncertain abt abstract n interface. I do understand that this is a common question posed during training and interview session but I just do not seem to get this right....Can someone pls provide a simple explanation and some examples if possible...I was reading one definition and its attached below.
---------------------------------------------------------------------------
Abstract
It is easy to overlook the abstract modifier and miss out on some of its implications. It is the sort of modifier that the examiners like to ask tricky questions about.

The abstract modifier can be applied to classes and methods. When applied to a method it indicates that it will have no body (ie no curly brace part) and the code can only be run when implemented in a child class. However there are some restrictions on when and where you can have abstract methods and rules on classes that contain them. A class must be declared as abstract if it has one or more abstract methods or if it inherits abstract methods for which it does not provide an implementation. The other circumstance when a class must be declared abstract is if it implements an interface but does not provide implementations for every method of the interface. This is a fairly unusual circumstance however.

If a class has any abstract methods it must be declared abstract itself.

Do not be distracted into thinking that an abstract class cannot have non abstract methods. Any class that descends from an abstract class must implement the abstract methods of the base class or declare them as abstract itself. These rules tend to beg the question why would you want to create abstract methods?

Abstract methods are mainly of benefit to class designers. They offer a class designer a way to create a prototype for methods that ought to be implemented, but the actual implementation is left to people who use the classes later on. Here is an example of an abstract a class with an abstract method. Again note that the class itself is declared abstract, otherwise a compile time error would have occurred.
---------------------------------------------------------------------------

Your help is very much appreciated.
 
Vinayak patil
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Suppose you have a class having methods whose behaviour depends on the sub classes inheriting them.
Ex: You have a Shape class which has a computeArea method. Now the area depends on the shape.
So you have a Circle class extending the Shape class, a Triangle class extending the Shape class etc.

If you provided a default implementation of the computeArea() method in the Shape class and you forgot to override it in the derived class, your results will be wrong.

So you declare the computeArea() method abstract and the Shape class as abstract. Now your Circle and Triangle classes have to provide an implementation of the method.

----
You might be knowing that java allows you to extend only one class. But if you want to use the functionality of more than one class, you use an interface.

Let us say you want to color your shapes as well and you want to write a color class so that it can be used to color other things too.
But you have already extended the Shape class so you cannot extend the Color class.
So you make it an interface and use it.

In an interface all methods are bodyless.

public interface Colors
{
public void getColor();
}

Hence your Circle class will be

public class Circle extends Shape
implements Color
{
}


Hope this helps

-Vinayak
 
Shafian Kisna
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your reply....

I am getting the gist of it. Will try to work out in my own examples and if I encounter any prob, I do hope to get some guidance.

Thanks once again....
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"KBav,"

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vinayak patil:
...sorry for my first reply. I really dont know how it got posted while I was typing my response...

Note: If you need to, you should be able to delete your own posts by clicking on the edit icon (paper & pencil) and checking the delete box.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!