Notes on classes and types of classes with runtime polymorphism example.


In this post we will learn about types of classes in java with examples. In this example we will develop runtime polymorphism

Class:
A class is a top level block that is used for grouping variables and methods for developing logic.

Types of classes;
    1) Interface
    2) Abstract class
    3) Concrete class
    4) Final class
    5) Enum

1) Interface;
It is fully unimplemented class used for declaring a set of operations of an object. It contains only public static final variables and public abstract methods. It is always created as root class. Interface tells what should implement but not how.

Let us consider an vehicle example, here we will create vehicle interface which tells what should we implement.


1
2
3
4
5
6
//Vehicle.java
interface Vehicle
{
       public void engine();
       public void breaks();
}

2) Abstract  class:
 It is partially implemented class used for developing some of the operations of an object. Abstract class is next level subclass. So it contains abstract methods, concrete methods including variables, blocks and constructors. It is always created as superclass next to interface.

In above we created vehicle as interface. So now here we will create bus, car, bike as abstract class, because we have different types of buses, cars, bikes with some common operarions and individual operations.


1
2
3
4
5
6
7
8
//Bus.java
abstract class Bus implements  Vehicles
{
      public void breaks()
      {
  System.out.println("Bus has two breaks");
      }
}

At bus level the method breaks() can only be implemented because it is common for all buses subclasses. The method engine() will be implemented in subclasses. So this class must be declared as abstract.

3) Concrete class;
 It is fully implemented class used for implementing all operations  of an object. It contains only concrete methods including variables, blocks and constructor. It is always created as subclass next to abstract class or interface in objects inheritance hierarchy.

In above we created Vehicle as interface and bus as abstract class which implements vehicle interface. So now here we will create RedBus, Volvo as concrete class by implementing all other operations which are left as abstract in its superclass(Bus.java).


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
//RedBus.java
class RedBus extends Bus
{
 public void engine()
 {
  System.out.println("RedBus engine capacity is 40 kmph");
 }
}
//Volvo.java
class Volvo extends Bus
{
 public void engine()
 {
  System.out.println("Volvo engine capacity is 100 kmph");
 }
}

So now develop a runtime polymorpic class to use all Vehicles operations


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Driver.java
class Driver
{
 public void assignVehicle(Vehicle v)
 {
  v.engine();
  v.breaks();
 }
}
//Depo.java
class Depo
{
 public static void main(String[] args)
 {
  Driver driver1=new Driver();
  driver1.assignVehicle(new Volvo());
  
                System.out.println("-------------------------------");

   Driver driver2=new Driver();
  driver1.assignVehicle(new Volvo());
 }
}

After running the appication output will be,











NOTE: If you want the above program in c#.net then visit http://csharphy.blogspot.in/

Download Option:


Comments