Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. /*
  2. In Java, you may write a lot of similar classes to do
  3. related, yet slightly different (or specialized) tasks.
  4. One way that you can reduce the amount of coding/class
  5. developing you do is to write a PARENT CLASS (or SUPER
  6. CLASS) that handles all SHARED/GENERIC tasks a class
  7. might perform, and write specialized CHILD CLASSES
  8. (or SUBCLASSES) that will handle the differing tasks.
  9. This process is known as INHERITANCE.
  10. The sub classes can inherit ALL or SOME of the parent
  11. class' properties, and then have some of their own
  12. unique properties.
  13. To have one class inherit another, we use the "extends"
  14. keyword.
  15. */
  16.  
  17. //Dog is a subclass of Animal, and Animal is the super
  18. //class of Dog.
  19. public class Dog extends Animal
  20. {
  21. //Member Variables
  22. //Because the Dog class extends (inherits) the Animal
  23. //class, it has ALL the member variable information
  24. //that is described in Animal.java
  25. //Therefore, without even writing anything, this class
  26. //has the member variables "vegetarian", "eats", and
  27. //"numOfLegs"
  28. private String breed;
  29. private String color;
  30.  
  31. //Default Constructor
  32. //To Construct the elements of the Dog class that were
  33. //inherited from the Animal super class, we must call
  34. //the constructor of the Animal class and provide it
  35. //with the proper values. To call a super class'
  36. //constructor, we use the keyword "super".
  37. public Dog()
  38. {
  39. super(false, "Meat", 4);
  40. breed = "Labrador";
  41. color = "black ";
  42. }
  43.  
  44. //Parameterized
  45. public Dog(boolean veg, String food, int legs, String bre, String col)
  46. {
  47. super(veg, food, legs);
  48. breed = bre;
  49. color = col;
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement