Sunteți pe pagina 1din 10

Inheritance Concepts

Example -1
Different kinds of objects often have a certain amount in common with each other. Mountain
bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current
speed, current pedal cadence, current gear). Yet each also defines additional features that make
them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop
handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.
Object-oriented programming allows classes to inherit commonly used state and behavior from
other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike,
and TandemBike. In the Java programming language, each class is allowed to have one direct
superclass, and each superclass has the potential for an unlimited number of subclasses:

Example -2
Let's say we make a class called "Human" that represents our physical characteristics. It's a
generic class that could represent you, me or anyone in the world. Its state keeps track of things
like number of legs, number of arms, and blood type. It has behaviors like eat, sleep, and walk.
Human is good for getting an overall sense of what makes us all the same but it can't for instance
tell me about gender differences. For that we'd need to make two new class types called "Man"
and "Woman". The state and behaviors of these two classes will differ from each other in a lot of
ways except for the ones that they inherit from Human.

Therefore inheritance allows us to encompass the parent class' state and behaviors into its child.
The child class can then extend the state and behaviors to reflect the differences it represents.
The most important aspect of this concept to remember is that the child class is a more
specialized version of the parent.

Example -3
Below are four java files. If you do not understand, code!
1. Animal.java
public class Animal {
public Animal() {
System.out.println("A new animal has been created!");
}

public void sleep() {


System.out.println("An animal sleeps...");
}

public void eat() {


System.out.println("An animal eats...");
}
}

2. Bird.java
public class Bird extends Animal {
public Bird() {
super();
System.out.println("A new bird has been created!");

@Override
public void sleep() {
System.out.println("A bird sleeps...");
}

@Override
public void eat() {
System.out.println("A bird eats...");
}
}

3. Dog.java
public class Dog extends Animal {
public Dog() {
super();
System.out.println("A new dog has been created!");
}

@Override
public void sleep() {
System.out.println("A dog sleeps...");
}

@Override

public void eat() {


System.out.println("A dog eats...");
}
}

4. MainClass.java
public class MainClass {
public static void main(String[] args) {
Animal animal = new Animal();
Bird bird = new Bird();
Dog dog = new Dog();
System.out.println();
animal.sleep();
animal.eat();
bird.sleep();
bird.eat();
dog.sleep();
dog.eat();
}
}

Output:
A new animal has been created!
A new animal has been created!
A new bird has been created!
A new animal has been created!
A new dog has been created!

An animal sleeps...
An animal eats...
A bird sleeps...
A bird eats...
A dog sleeps...
A dog eats...

Finally,
IS-A Relationship:
In object oriented programming, the concept of IS-A is a totally based on Inheritance, which can
be of two types Class Inheritance or Interface Inheritance (will be covered in next lab). It is just
like saying "A is a B type of thing". For example, Apple is a Fruit, Car is a Vehicle etc.
Inheritance is uni-directional. For example House is a Building. But Building is not a House.
It is key point to note that you can easily identify the IS-A relationship. Wherever you see an
extends keyword or implements keyword in a class declaration, then this class is said to have ISA relationship.

HAS-A Relationship:
We will study in some other lab sessions.

Practice Questions
1. Level: Beginner

Create a superclass, Student, and two subclasses, Undergrad and Grad.


The superclass Student should have the following data members:
name, ID, grade, age, and address.
The superclass, Student should have at least one method: boolean
isPassed (double grade)
The purpose of the isPassed method is to take one parameter, grade
(value between 0 and 100) and check whether the grade has passed the
requirement for passing a course. In the Student class this method
should be empty as an abstract method.
The two subclasses, Grad and Undergrad, will inherit all data members
of the Student class and override the method isPassed. For the
UnderGrad class, if the grade is above 70.0, then isPassed returns true,
otherwise it returns false. For the Grad class, if the grade is above 80.0,
then isPassed returns true, otherwise returns false.
Create a test class for your three classes. In the test class, create one
Grad object and one Undergrad object. For each object, provide a grade
and display the results of the isPassed method.

2. Level: Intermediate

Super Class
Employees at DreamWorks Ltd typically work 40 hours a week. The employees are paid $40000
a year. They get 2 weeks of paid vacation. They need to fill a vacation form to apply for a leave.
The color of vacation forms are different for different employees. However, like in nearly all
other organizations, there are special employees who are paid more or less than the standards at
DreamWorks. For example, Lawyers are paid more while janitors are paid less.
public class Employee {
public int getHours() {
return 40;

// works 40 hours / week

public double getSalary() {


return 40000.0;

// $40,000.00 / year

public int getVacationDays() {


return 10;

// 2 weeks' paid vacation

public String getVacationForm() {


return "yellow";

// use the yellow form

}
}

Subclass: Lawyer
public class Lawyer extends Employee {

public int getVacationDays() {


return super.getVacationDays() + 5;

// 3 weeks vacation

public String getVacationForm() {


return "pink";
}

public void sue() {


System.out.println("I'll see you in court!");
}
}

Subclass: Secretary
public class Secretary extends Employee {
public double getSalary() {
return super.getSalary() + 5000.0;

// $45,000.00 / year

public void fileBriefs() {


System.out.println("I could file all day!");
}
}

Tasks:
1. Write a class Janitor. Janitors work twice as many hours (80 hours/week), they make
$30,000 ($10,000 less than others), they get half as much vacation (only 5 days), and they
have an additional method named clean that prints "Working tirelessly for you."

2. If you finish, modify it to use the super keyword to connect with the Employee superclass
as appropriate.
Hint: a. See the use of super keyword in Lawyer and LegalSecretary classes.
b. 2 * super.getHours(), super.getSalary() - 10000.0, super.getVacationDays() / 2

Happy Coding!!!

S-ar putea să vă placă și