Advertisement
apad464

Inheritance_ex2.java

Jan 18th, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. class Price
  2. {
  3.     private double price;
  4.     public Price(double p){price = p;}
  5.     public double getPrice(){return price;}
  6.     public String toString(){return "Price is " + getPrice();}
  7. }
  8.  
  9. class DiscountPrice extends Price
  10. {
  11.     public DiscountPrice(double p){super(p);}
  12.     public double getPrice(){return super.getPrice()*.85;}
  13.     /* copied from the Price class
  14.         private double price;
  15.         public Ticket(double p){price = p;}
  16.         public double getPrice(){return price;}
  17.         public String toString(){return "Price is " + getPrice();}
  18.     */
  19.    
  20. }
  21.  
  22. public class Inheritance_ex2
  23. {    
  24.     public static void main(String[] args)
  25.     {
  26.         Price t = new Price(2500);
  27.         System.out.println(t);  
  28.            
  29.         System.out.println("\n");  
  30.            
  31.         Price t2 = new DiscountPrice(2500);
  32.         System.out.println(t2);
  33.            
  34.         System.out.println("\n");      
  35.     }  
  36.    
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement