OOP Case Study
OOP Case Study
Case Study
in
Object Oriented
Programming
Submitted By:
Submitted To:
The idea behind inheritance in Java is that you can create new classes that are built upon
existing classes. When you inherit from an existing class, you can reuse methods and fields of
the parent class. Moreover, you can add new methods and fields in your current class also [1].
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates that you are making a new class that derives from an
existing class. The meaning of "extends" is to increase the functionality.
Example:
Figure 1 Sample
As displayed in the above figure, Programmer is the subclass and Employee is the superclass.
The relationship between the two classes is Programmer IS-A Employee. It means that
Programmer is a type of Employee [2].
In the above example, Programmer object can access the field of own class as well as of
Employee class i.e. code reusability.
B. Case Analysis
B.1 Problem
Create a program with Dog class that inherits the Animal class.
B.2 Solution/Implementation
class Animal {
String name;
class Main {
shipoo.name = "Ely";
shipoo.display();
shipoo.age();
shipoo.eat();
}
B.3 Explanation
In our solution above, we have constructed a subclass Dog from superclass Animal. Shipoo is
an object of Dog. However, age and eat() are the attributes of the Animal class. Since Dog
inherits the field and method from Animal, we are able to access the field and method using the
object of the Dog.
C. References