Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- In Java, you may write a lot of similar classes to do
- related, yet slightly different (or specialized) tasks.
- One way that you can reduce the amount of coding/class
- developing you do is to write a PARENT CLASS (or SUPER
- CLASS) that handles all SHARED/GENERIC tasks a class
- might perform, and write specialized CHILD CLASSES
- (or SUBCLASSES) that will handle the differing tasks.
- This process is known as INHERITANCE.
- The sub classes can inherit ALL or SOME of the parent
- class' properties, and then have some of their own
- unique properties.
- To have one class inherit another, we use the "extends"
- keyword.
- */
- //Dog is a subclass of Animal, and Animal is the super
- //class of Dog.
- public class Dog extends Animal
- {
- //Member Variables
- //Because the Dog class extends (inherits) the Animal
- //class, it has ALL the member variable information
- //that is described in Animal.java
- //Therefore, without even writing anything, this class
- //has the member variables "vegetarian", "eats", and
- //"numOfLegs"
- private String breed;
- private String color;
- //Default Constructor
- //To Construct the elements of the Dog class that were
- //inherited from the Animal super class, we must call
- //the constructor of the Animal class and provide it
- //with the proper values. To call a super class'
- //constructor, we use the keyword "super".
- public Dog()
- {
- super(false, "Meat", 4);
- breed = "Labrador";
- color = "black ";
- }
- //Parameterized
- public Dog(boolean veg, String food, int legs, String bre, String col)
- {
- super(veg, food, legs);
- breed = bre;
- color = col;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement