Sunday, July 22, 2012

Abstract Classes: Runtime Polymorphism



Before reading this post, I recommend you to go through Runtime Polymorphism since this post is an extension of the same.

Consider an organization where an Employee can be a Regular Employee or a Consultant. After all, an employee cannot be just an employee. An employee is under one of these categories. To simulate these types of cases in OOP, we make use of abstract classes in Java.

An abstract method (pure virtual function in C++) doesn’t contain a body. An abstract method must be overridden in subclass. Otherwise, the subclass will become abstract.

A class having one or more abstract functions can be declared as an abstract class. We cannot create an object for an abstract class. However, an object reference for an abstract class can be created. Now you may doubt the use of creating a class without an object. For example, in the above example, an object of the Employee class should not be created since an employee cannot just be an employee. Such types of classes should be defined as abstract classes. Now if we allow the objects to be created for an Employee class, it might lead to extra memory usage and also might defeat the very purpose of runtime polymorphism. But we can create an object reference of Employee and point it to an object of its subclass. This is where runtimepolymorphism again comes into picture. In the previous post, you must have already read the golden rule of Runtime Polymorphism: “An object reference of a super class can always point to an object of a subclass.” Instead of the super class object reference pointing to an object of subclass, we now make the abstract class object reference pointing to an object of a subclass.

The implementation for Employee Class is given below.


The implementation for RegularEmployee class is given below.



The implementation for Consultant class is given below.



You can observe that the abstract method in the superclass is overridden in both the above subclasses. Now we will see how runtime polymorphism can be applied using abstract classes.



A class can be declared as an abstract class even though it doesn’t contain any abstract methods. This can be used in situations where there are no abstract functions to be overridden in the subclasses, but at the same time, the abstract class should not have an object.

An abstract class can contain other non-abstract members such as methods and instance variables.

Related Posts:

Sunday, July 8, 2012

Essence of OOP: Runtime Polymorphism in Java

This post does not give the definitions of Runtime polymorphism or Dynamic method dispatch. Instead, it helps Java beginners (or any OOP Beginner) to use runtime polymorphism. I personally think Inheritance and Runtime polymorphism forms the crust of OOP (not just in Java but in all other OOP Languages also).

The entire dynamic method dispatch is based on the rule that an object reference of the super class can refer to an object of any subclass in the hierarchy. This can be explained with the following example.

Consider there are two classes Person and Faculty. Person class is the super class of Faculty class.

Since superclass and subclass share IS A relationship, every Faculty is a Person. So, whenever Java expects Person, a Faculty can be used. This kind of implicit casting is called upcasting.



Unlike in upcasting, every Person need not be a Faculty. Thus, if we use Person in place of Faculty, explicit casting needs to be done. Therefore, we need to use explicit typecasting in order to perform this operation. This kind of explicit casting is called as downcasting.



In runtime polymorphism, the call to an overridden method is resolved at runtime instead of compile time. It occurs when an overridden method is called using an object reference of superclass. Remember that you can only call methods which are present in the super class.

Now let us extend the previous mentioned example with another subclass to "Person" class: NonTeaching class. Now the class diagram is as follows:

The below class demonstrates the runtime polymorphism for the above class diagram.



As can be seen from the code above, Java calls appropriate method depending on the object pointed to by the object reference. In the first case, the object reference p is pointed to the object of Faculty class. So, the printDetails() from the Faculty class is called. In the second case, p points to object of NonTeaching class. Thus, the printDetails() from the NonTeaching class is called.

Always remember that the dynamic method dispatch is applied only to method that is defined in the super class, overridden in subclass and invoked using an object reference of superclass.
Related Posts Plugin for WordPress, Blogger...