Showing posts with label Java Abstract Classes. Show all posts
Showing posts with label Java Abstract Classes. Show all posts

Sunday, 31 May 2015

Java Abstract Classes

There are situations in which you will want to define a super class that declares the structure of a given abstraction without providing a complete implementation of every method. That is, sometimes you will want to create a super class that only defines a generalized form that will be shared by all of its subclasses, leaving it to each sub class to fill in the details. Such a class determines the nature of the methods that the sub classes must implement. One way this situation can occur is when a super class is unable to create a meaningful implementation for a method.

This is the case with the class Shape used in the preceding example. The definition of area() is simply placeholder. It will not compute and display the area of any type of object. When you create your own class libraries, it is not uncommon for a method to have no meaningful definition in the context of its super-class.

You can handle this situation in two ways: 

One way, as shown in the previous example, is to simply have it report a warning message. While this approach can be useful in certain situations- such as debugging. It is not usually appropriate. You may have methods, which must be overridden by the sub class in order for the sub class to have any meaning.

Consider the class triangle. It has no meaning if area() is not defined. In this case, you want some way to ensure that a sub class does, indeed, override all necessary methods.

Java’s solution to this problem is the abstract method. You can require that certain methods be overridden by subclasses by specifying the abstract type modifier. These methods are sometimes referred to as subclass responsibility because they have no implementation specified in the super class. Thus a sub class must override them – it can not simply use the version defined in the super class. To declare an abstract method, use this general form:

abstract return_type name(parameter-list);

As you can see, no method body is present. Any class that contains one or more abstract methods must also be declared abstract. To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the beginning of the declaration.

There can be no object of an abstract class. That is, an abstract class can not be directly instantiated with the new operator. Such objects would be useless, because an abstract class is not fully defined. Also you cannot declare abstract constructors or abstract static methods. Any subclass of an abstract class must either implement all of the abstract methods in super class, or be itself declared abstract. Here, is a simple example of a class with an abstract method, followed by a class which implements that method.

Example:

1 abstract class Shape //abstract class

2 { double puCost = 100;

3 abstract double area(); //abstract method.

4 double calCost()

5 {  return puCost * area();

6 }

7 }

1 class Triangle extends Shape

2 {

3 double s1,s2,s3;

4 Triangle(double a, double b, double c)

5 {

6   s1 = a; s2 = b; s3 = c;

7 }

8 double area()

9 {

10 double s = (s1+s2+s3)/2;

11 return Math.sqrt(s*(s-s1)*(s-s2)*(s-s3));

12 }

13 }

1 class Rectangle extends Shape

2 { double s1,s2;

3 Rectangle(double a, double b)

4 {

5  s1 = a; s2 = b;

6 }

7 double area()

8 {

9 return s1 * s2;

10 }

11 }

1 class Square extends Shape

2 {

3 double s;

4 Square(double a)

5 {

6   s = a;

7 }

8 double area()

9 {

10 return s * s;

11 }

12 }

1 class ShapeTest1

2 { static public void main(String args[])

3     { //Shape s = new Shape();

4   Shape s1 = new Triangle(3,4,5);

5   Shape s2 = new Rectangle(3,4);

6   Shape s3 = new Square(3);

7   //System.out.println(s.area());

8   System.out.println(s1.area());

9   System.out.println(s2.area());

10   System.out.println(s3.area());

11   //System.out.println(s.calCost());

12   System.out.println(s1.calCost());

13   System.out.println(s2.calCost());

14   System.out.println(s3.calCost());

15 }

16 }

Output:

6.0
12.0
9.0
600.0
1200.0
900