khirulnizam

Inheritance example

Jan 29th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. //////// super-class Book
  2. public class Book {
  3. String title;
  4. double price;
  5.  
  6. Book(String newtitle){
  7. title=newtitle;
  8. }
  9.  
  10. public String getTitle(){
  11. return title;
  12. }
  13.  
  14. public double getPrice(){
  15. return price;
  16. }
  17.  
  18. public void setPrice(double newPrice){
  19. price=newPrice;
  20. }
  21. }
  22.  
  23.  
  24. //////// sub-class Fiction
  25. public class Fiction extends Book {
  26.  
  27. public Fiction(String fiction){
  28. super(fiction);
  29. setPrice();
  30. }
  31. public void setPrice(){
  32. super.price= 24.99;
  33. }
  34. }
  35.  
  36. //////// sub-class nonFiction
  37.  
  38. public class nonFiction extends Book{
  39.  
  40. public nonFiction(String nonfiction){
  41. super(nonfiction);
  42. setPrice();
  43. }
  44. public void setPrice(){
  45. super.price = 37.99;
  46. }
  47. }
  48.  
  49. //////// class tester/usage/implementation
  50. // bit.ly/oopkerul
  51. // LAB4
  52.  
  53. public class useBook {
  54. public static void main(String[]args){
  55.  
  56. nonFiction obj1=new nonFiction("Sejarah SPM");
  57. Fiction obj2=new Fiction("Armagedon");
  58.  
  59. System.out.println(
  60. "The title is "+obj1.getTitle()+
  61. " and the price is "+obj1.getPrice());
  62. System.out.println(
  63. "The title is "+obj2.getTitle()+
  64. "and the price is "+obj2.getPrice());
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment