Quiz abstract classes in Java: call from a concrete method to an abstract one

| June 25, 2012 | 0 Comments

 

Understanding how works abstract class and the differences with an interface is very important if you want to make a good OOP design in Java. Basically an abstract class is a class that is declared as abstract, it can include both abstract and not abstract methods. Abstract classes cannot be instantiated but they can be subclasses.

The abstract class are used frequently to define a superclass with methods that can be implemented in the same class or can be delegated them implementation to the subclass. On the other hand, if you use an interface you have a list of methods not implemented that have to be implemented by each subclass that implements the interface. If you need a comparison about the use of interfaces versus abstract class I recommended you read the post When to use an abstract class and an interface.

Quiz: ¿What happen with the next code?

Suppose that you have the next code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package nl.mergetag.examples;
/**
*
* Abstract class that represent a generic graphic object
*
* @version 1.0
* @author ftrujillo 
* */
public abstract class GraphicObject {
int x, y = 0;
/*** concrete method ***/
void moveTo(int newX, int newY) {
//call to abstract method
draw();
resize();
 
}
//list of absttract methods
abstract void draw();
abstract void resize();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package nl.mergetag.examples;
/**
 *
 * Class that represents a circle that is a Graphic Object
 *
 * @version 1.0
 * @author ftrujillo
 *
 */
class Circle extends GraphicObject {
     void draw() {
        //implements of abstract method
    }
     void resize() {
       //implements of abstract method
    }
}

You have an abstract class GraphicObject which have three methods. From the concrete method we call both abstract methods and after that, we use a main class to instantiate the concrete class Circle and invoke the concrete method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package nl.mergetag.examples;
 
/**
 * Principal class to make test.
 *
 * @author ftrujillo
 * @version 1.0
 *
 */
public class Execute {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Circle circleInst = new Circle();
		//call the concrete method defined in the abstract class
		circleInst.moveTo(1, 1);
		circleInst.draw();
	}
 
}

¿Compile and run the application?

Answer: Yes

The example compiles and run without generating error or exception. The reason for that is that the implementation of the method is decided in runtime. The reason for that is Java use dynamic binding for overriding methods, which is the case when you implemented an abstract class. And it is in runtime when the Java virtual machine decided which the implementation of the method is.

————————————————————————————

References:

Differences betweeen use of interfaces and abstract classes.

Official doc about abstract methods in Oracle Docs.

 

Tags: ,

Category: Java

Leave a Reply