1+ //Program to design a vehicle class hierarchy in Java, and develop a program to demonstrate Polymorphism.
2+ class Vehicle
3+ {
4+ String regno ;
5+ int model ;
6+ Vehicle (String r , int m )
7+ {
8+ regno = r ;
9+ model = m ;
10+ }
11+
12+ void display ()
13+ {
14+ System .out .print ("\n Registration no: " +regno );
15+ System .out .print ("\n Model no: " +model );
16+ }
17+ }
18+
19+ class Twowheeler extends Vehicle
20+ {
21+ int noofwheels ;
22+ Twowheeler (String r , int m , int n )
23+ {
24+ super (r ,m );
25+ noofwheels = n ;
26+ }
27+
28+ void display ()
29+ {
30+ System .out .print ("\n Two wheeler tvs" );
31+ super .display ();
32+ System .out .print ("\n No. of wheels: " + noofwheels );
33+ }
34+ }
35+
36+ class Threewheeler extends Vehicle
37+ {
38+ int noofwheels ;
39+
40+ Threewheeler (String r ,int m ,int n )
41+ {
42+ super (r ,m );
43+ noofwheels = n ;
44+ }
45+
46+ void display ()
47+ {
48+ System .out .print ("\n Three wheeler auto" );
49+ super .display ();
50+ System .out .print ("\n No. of wheels: " +noofwheels );
51+ }
52+ }
53+
54+ class Fourwheeler extends Vehicle
55+ {
56+ int noofwheels ;
57+
58+ Fourwheeler (String r ,int m ,int n )
59+ {
60+ super (r ,m );
61+ noofwheels =n ;
62+ }
63+
64+ void display ()
65+ {
66+ System .out .print ("\n Four wheeler car" );
67+ super .display ();
68+ System .out .print ("\n No. of wheels: " + noofwheels );
69+ }
70+ }
71+
72+ class PolymorphismProgram
73+ {
74+ public static void main (String arg [])
75+ {
76+ Twowheeler t1 = new Twowheeler ("KA74 12345" , 1 ,2 );
77+ Threewheeler th1 = new Threewheeler ("KA74 54321" , 4 ,3 );
78+ Fourwheeler f1 = new Fourwheeler ("KA34 45677" ,5 ,4 );
79+ t1 .display ();
80+ th1 .display ();
81+ f1 .display ();
82+ }
83+ }
0 commit comments