Advertisement
steverobinson

Inheritance Super Keyword

Aug 5th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. //Program to implement inheritance with super keyword
  2.  
  3. class Vehicle
  4. {
  5.     private String regNo;
  6.     private int model;
  7.     private String color;
  8.    
  9.     public Vehicle(String regNo,int model,String color)
  10.     {
  11.         this.regNo=regNo;
  12.         this.model=model;
  13.         this.color=color;
  14.     }
  15.    
  16.     public String toString()
  17.     {
  18.         String out="Registration: "+regNo+"\nModel: "+model+"\nColor: "+color;
  19.         return out;
  20.     }
  21. }
  22.  
  23. class Car extends Vehicle
  24. {
  25.     private String type;
  26.     private String company;
  27.    
  28.     public Car(String regNo,int model,String color,String type,String company)
  29.     {
  30.         super(regNo,model,color);
  31.         this.type=type;
  32.         this.company=company;
  33.     }
  34.    
  35.     public String toString()
  36.     {
  37.         String out=super.toString();
  38.         out+="\nType: "+type+"\nCompany: "+company;
  39.         return out;
  40.     }
  41. }
  42.  
  43. class InheritanceTest
  44. {
  45.     public static void main(String a[])
  46.     {
  47.         Vehicle veh1=new Car("TN 20 H 108",2008,"Silver","Sedan","Toyota");
  48.         System.out.println("Details of the Car: \n"+veh1);
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement