Using Abstract Classes Assignment Help

Assignment Help: >> Polymorphism in Java - Using Abstract Classes

Using Abstract Classes

There are conditions in that you will need to define a superclass which declares the structure of a provided abstraction without giving a complete implementation of each method. That is many times you will need to create a superclass which only defines a generalized form that will be shared through all of its subclasses, leaving it to every subclass to fill in the details. As like a class determines the nature of the methods in which the subclasses must implement. A single way of this situation can occur is when a superclass is unable to create a meaningful implementation for a method. This is the case along with the class figure used in the preceding example. A definition of area ( ) is simply a placeholder. That will not compute and show the area of any type of object. As you will see as you create your own class libraries, it is not uncommon to a method for have no useful definition in the context of its superclass. You can handle this condition two ways. One way, as display in the previous instance, is to simply have it report a warning message. Although this approach can be useful in certain locations such as debugging it is not commonly suitable. You might have methods that must be overridden through the subclass in order for the subclass to have any meaning. Let consider the class Triangle. It has no meaning if area ( ) is not described. In that case, you need some way to ensure in which a subclass does, indeed and override all necessary methods.  Java's solution to this problem is the abstract method.

You can need which certain methods be overridden through subclasses by specifying the abstract type modifier. These techniques are many times referred to a subclasses responsibility since they have no implementation specified in the superclasses . Therefore, a subclass must override them-it cannot easily use the version defined in the superclass. For declare abstract methods, use this common form.

abstract type name (parameter-list);

As you could see, no method body is present.

Any class which holds one or more abstract methods have also be declared abstract. For declare a class abstract, you simply use the abstract keyword in front of the class keyword at the starting of the class declaration. There could be no objects of that class. That is, an abstract class cannot be directly instantiated along with the new operator. Like as objects will be useless, since an abstract class is not fully defined. Also, you cannot declare abstract constructors, or abstract static methods. A few subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself defines abstract.

Here is easy example of class along with an abstract method, followed through a class that implements which method;

/ / A simple demonstration of abstract.

abstract class A {

abstract void callme ( ) ;

/ / concrete technique are still permitted in abstract classes

void callmetoo ( ) {

System.out.println ("This is a concrete method.");

}

}

class B extends A {

void callme ( ) {

system.out.println ("B' s implementation of callme.");

}

}

class AbstractDemo {

public static void main (string args [ ] ) {

B b = new B ( ) ;

b. callme ( );

b.callmetoo ( );

}

}

Note that no objects of class A are define in the program. As described, it is not possible to instantiated an abstract class. Another point, class A implements a concrete method known as callmetoo ( ). This is perfectly acceptable. A Abstract classes could involve as much implementation as they see fit.

Although abstract classes cannot be used to instantiate objects, they could be used to create object references, since Java's approach to run_time polymorphism is implemented by the use of superclass references. Therefore, it must be possible to make a reference to an abstract class so which it can be used to point to a subclass object. You will see this characteristics put to use in the further example.

By using an abstract class, you can improve the Figure class display earlier. Because there is no useful concept of area for an underlined 2D Figure, the subsequent version of the program devalues area ( ) as abstract within Figure. That, of course, means that all classes derived from Figure must override area ( ).

/ / using abstract methods an classes.

abstract class figure {

double dim1;

double dim2;

Figure (double a, double b) {

dim = a;

dim2 = b;

}

/ / area is now an abstract method

abstract double area ( ) ;

}

class Rectangle extends Figure {

Rectangle ( double a, double b) {

super (a, b ) ;

}

/ / overide area for rectangle double are ( ) {

system.out.println ( "Inside Area for Rectangle.");

return dim1 * dim 2;

}

}

class Traingle extends Figure { T

raingle ( double a, double b) {

super (a,         b );

}

/ / override area for right triangle

double area ( ) {

System.out.println( "Inside Area for Triangle.");.

retrun.dim1 * dim2 / 2 ;

}

}

class AbstractAreas {

public static void main (String args [ ] ) {

/ / Figure f = new Figure ( 10,10 ); / / illegal now

Rectangle r = new Rectangle ( 3,9 );

Triangle t = new Triangle ( 10, 8 );

Figure figref; / / this is ok, no object is created

 figref = r;

System.out.println ("Area" + figref.area ( ) );

figref = t;

System.out.println ("Area" + figref.area ( ) );

}

}

As the comment inside main ( ) denotes, it is no longer possible to declare objects of type figure, because it is now abstract. And, all subclasses of the Figure class must override area ( ). To show this to yourself, try creating a subclass which does not override area ( ). You will receive a compile- time error..

While it is not possible to create an object of type Figure, you could create a reference variable for type Figure. A variable Figref is declared as a reference to Figure that means in which it could be used to refer to an object of any class derived from Figure. As explained, it is by superclass reference variables which overridden methods are resolved at run time.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd