Website Design United States, Website Design California, Website Designing United States, Website Designing California

Abstract Class Tutorial

Abstract classes are a lot like Interfaces. They can be used to define a data type just like an Interfaces does. The one main difference is that abstract classes can have code in them. Q: Why didn't you cover abstract classes after interfaces? A: Abstract classes won't work without inheritance so it was necessary to learn about inheritance first. Q: What distinguishes an abstract class from a regular class? A: An abstract class can not be directly made an object of (with the "new" keyword) and therefore must be inherited.

It can still be used as a data type like an interface, just not created. The greatest part of this is unlike an interface you can have code and therefore anything that would be common to all that inherit it could just be coded in and inherited. Anything that wouldn't be common could easily be overridden or just defined like an interface.

To illustrate this we will make an abstract class of what Square, Triangle, Rectangle, and Cube all have in common. Well they are all Shapes. Some simple commonalities are they all have a name and number of sides.

There's not a whole lot new in abstract classes that you wouldn't already know from the previous three tutorials so this will be quite short. We will continue on using our classes from the previous lessons.

Create a new .java file and call it Shape. To make a class abstract we simply use abstract in the class declaration.

public abstract class Shape
{

}

So the two function that the Shape class will be are getShapeName and getNumOfSides. Now for getShapeName we can supply some default code to return the name but we will allow them to override it if they wish. As for getNumOfSides we will not supply any code and just have our inheritors do that. To declare a function that has no code in it, it is declared as abstract.

public abstract class Shape
{
public String getShapeName()
{
return this.getClass().getName();
}

public abstract int getNumOfSides();
}

Now time to inherit this class. A simple example will be the Triangle class where we will just use the Shape's getShapeName and override the getNumOfSides. The only changes/additions are:

public class Triangle extends Shape
{
public int getNumOfSides()
{
return 3;
}

For the original Square class we will be a little different and override the getShapeName function just to show an example.

public class Square extends Shape
{
public String getShapeName()
{
return "A Square";
}

public int getNumOfSides()
{
return 4;
}

Since Square2 and Rectangle implement an Interface they can still inherit Shape and the code would look similar to the two examples above.

Just like an Interface and inherited creatable Classes you can use the Abstract class as a data type and therefore you could have two Shape variables set to a Triangle and a Square. Return to the Tester class and do just that.

public static void main(String[] args)
{
Shape t, s;
t = new Triangle();
s = new Square();
System.out.println("t is a " + t.getShapeName() + " with " +
t.getNumOfSides() + " sides");
System.out.println("s is " + s.getShapeName() + " with " + s.getNumOfSides() +
" sides and an area of " + ((Square)s).area());
}

Output:
t is a Triangle with 3 sides
s is A Square with 4 sides and an area of 1.0

Notice that the Square's getShapeName returned "A Square" because we overwrote that function and that's what we decided to return.

Congratulations you have completed the Abstract Class lesson. You may not continue on to the Instance vs. Class lesson.

Author Information:

Adam Schentag

http://www.programmers-corner.com

adam@programmers-corner.com

Comments:

Add your comments here.

Name

Comment

You can also send feedback to feedback@programmers-corner.com

There are currently no comments available.















 


© 2008-2009 dotnet4all.com