Advertisement
yo2man

Tutorial 37 Abstract Classes

May 24th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. //Tutorial 37 (Abstract Classes)
  2. //Abstract classes are something you can use when you want to start creating a class hierarchy.
  3. //But the base class is not going to be doing anything in itself, its only gonna act as base for other classes.
  4. //Machine is a base class
  5.  
  6. //App.java
  7. public class App {
  8.  
  9.     public static void main(String[] args) {
  10.         Camera cam1 = new Camera();
  11.             cam1.setId(5);
  12.        
  13.         Car car1 = new Car();
  14.             car1.setId(4);
  15.             car1.run();
  16.            
  17.     //Machine machine1 = new Machine(); //Error: "cannot instantiate type Machine" see Machine.java comment
  18.                
  19.     }
  20.  
  21. }
  22.  
  23. //---------------------------------------------------------------------------------------------------------------------------------------
  24. //Machine.java
  25.  
  26. public abstract class Machine { //adding "abstract" to "public class Machine {" prevents the user of this heirarchy from creating the base parent class, in this case: Machine
  27.     //abstract classes can have abstract methods
  28.  
  29.    
  30.    
  31.     private int id;
  32.         //then right click>generate getters and setters>check mark id> boom:
  33.     public int getId() {
  34.         return id;
  35.     }
  36.  
  37.     public void setId(int id) {
  38.         this.id = id;
  39.    
  40.     }
  41.    
  42.     public abstract void start(); //make methods abstract and with "@Override" to make child classes implement them
  43.     public abstract void doStuff();
  44.     public abstract void shutUp();
  45.  
  46.    
  47.     public void run(){
  48.         start();
  49.         doStuff();
  50.         shutUp();
  51.     }
  52. }
  53. //---------------------------------------------------------------------------------------------------------------------------------------
  54.  
  55. public class Camera extends Machine {
  56.  
  57.     @Override
  58.     public void start() {
  59.         System.out.println("Starting camera.");
  60.     }
  61.  
  62.     @Override
  63.     public void doStuff() {
  64.         System.out.println("Taking a photo");
  65.        
  66.     }
  67.  
  68.     @Override
  69.     public void shutUp() {
  70.         System.out.println("Shutting down the camera.");
  71.        
  72.     }
  73.  
  74. }
  75.  
  76. //---------------------------------------------------------------------------------------------------------------------------------------
  77.  
  78. public class Car extends Machine { //when you say "Car extends Machine", you are making a very strong statement that "Car is a machine.
  79.  
  80.     @Override
  81.     public void start() {
  82.         System.out.println("Starting camera.");
  83.     }
  84.  
  85.     @Override
  86.     public void doStuff() {
  87.         System.out.println("Do stuff in car.");
  88.        
  89.     }
  90.  
  91.     @Override
  92.     public void shutUp() {
  93.         System.out.println("Shut up Optimus Prime.");
  94.        
  95.     }
  96.  
  97. }
  98.  
  99.  
  100.  
  101.  
  102. //---------------------------------------------------------------------------------------------------------------------------------------
  103. /*Run Results:
  104. Starting camera.
  105. Do stuff in car.
  106. Shut up Optimus Prime.
  107. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement