0% found this document useful (0 votes)
63 views10 pages

09CSA Unit9.1 CreatingSuperclassesAndSubclasses Key

The document explains the concept of inheritance in Java, detailing the relationship between superclasses and subclasses. It outlines the benefits of inheritance, such as code reusability and maintainability, and provides examples of class hierarchies, specifically in the context of performers in a simulation game. Additionally, it includes practice exercises for creating classes and understanding class relationships.

Uploaded by

rizvifz27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views10 pages

09CSA Unit9.1 CreatingSuperclassesAndSubclasses Key

The document explains the concept of inheritance in Java, detailing the relationship between superclasses and subclasses. It outlines the benefits of inheritance, such as code reusability and maintainability, and provides examples of class hierarchies, specifically in the context of performers in a simulation game. Additionally, it includes practice exercises for creating classes and understanding class relationships.

Uploaded by

rizvifz27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🔙 to cover

to cover

Unit 9
Unit 9
Inheritance

1
Dimla
9.1-Creating Superclasses and
🔙 to divider

to divider
Subclasses
Objectives:
• I can create an inheritance relationship form a subclass to the superclass.

Question: What is Inheritance in Java?

Answer: Inheritance in Java is creating new classes based on existing class(blueprint). It is


derived from a subclass that acquires properties and behaviors from an existing class
called superclass or the base class.

It is used to build a hierarchy of classes that have similar characteristics.

Question: What is a Superclass in Java?

Answer: Superclass is also known as a parent class or base class. It is the class being inherited
from.

Question: What is a Subclass in Java?

Answer: Subclass is also known as a derived class or child class. It is the class that inherits the
properties and methods from the superclass.

2
Dimla
9.1-Creating Superclasses and
🔙 to divider

to divider
Subclasses

Question: Why do we use inheritance in Java?


Answer: Here are the benefits of inheritance:

1) Code reusability: Higher-level classes can be used repeatedly in many solutions.


2) Prevents repeating code: Common methods and variables are now in one location rather than
many.
3) Readability and organization: Having a solid, organized structure of your classes and objects
allows for greater readability and cohesion.
4) Maintainability: Changing a general behavior in a superclass(parent class) automatically apply to
subclass(child class). This reduces the risk of errors and makes updates ore efficient.

Example:
Consider a scenario where you are designing a simulation game. Among the many different types
of characters in the game, there are performers. Performers have a name, an age, a hometown,
and an agent. All performers can practice and perform. There are also many different types of
performers, including musicians, comedians, dancers, and hip-hop dancers. Each type of
performer will have the common performer characteristics, but they will also have specific
attributes and behaviors that relate to their individual group.
Subclasses(child class)
Superclass(parent class) Note: Each subclass can only have one superclass.
public class Performer public class Musician extends Performer

private String name;


private String instrument;
private int age;
private String hometown;
private String agent; getInstrument()
playIntrument()
Common Methods
getName() setName()
getAge() setAge() public class Comedian extends Performer
getHometown() setHometown()
getAgent() setAgent() private String jokes;
toSting()
practice() writeJokes()
perform()

Note: All subclasses inherit the attributes and methods of their superclasses.

public class Dancer extends Performer public class HiphopDancer extends Dancer

private int shoeSize; private String outfit;

dance() levelDifficulty() 3
selectMusic() Dimla
9.1-Creating Superclasses and
🔙 to divider

to divider
Subclasses

Superclasses and Subclasses

Performer

Musician Comedian Dancer

HiphopDancer

How does inheritance work? Based on the hierarchy diagram, we can say…

• Musician is a subclass of the Performer superclass.

• Comedian is a subclass of the Performer superclass.

• Dancer is a subclass of the Performer superclass.

• HiphopDancer is a subclass of the Dancer superclass.

Question: How do we know which way the arrows go??

Answer: Using the “is-a” method

• A Musician is a Performer YES NO

• A Performer is a Dancer YES NO

• A HipHopDancer is a Dancer YES NO

• A Musician is a Comedian YES NO

• A Performer is a Musician YES NO

• Comedian is a Performer YES NO


4
• A HiphopDancer is a Performer YES NO Dimla
9.1-Creating Superclasses and
🔙 to divider

to divider
Subclasses

9.1-Practice1
Directions: Multiple Choice Questions, choose the correct answer.

In a particular school system, there will be two types of people: students and teachers. Both types of
people will have names, ages, addresses, and ID numbers. Students will also have grade level and
GPA, while teachers will have a hire date and salary.

Which of the following would be the best class design for this scenario?

A. Three independent, unrelated classes: Person, Student, and Teacher


B. A Person superclass, and two subclasses of Person: Student and Teacher
C. A Person superclass. Teacher is a subclass of Person, and Student is a subclass of
Teacher
D. A Student superclass. Teacher is a subclass of Student, and Person is a subclass of
Teacher
E. Two superclasses: Teacher and Student, with Person as a subclass of each

Answer: B
Directions: True or False.

1) A Sandwich is a Lunch Item True False

2) A Drink is a Soda True False

3) A Hamburger is a Lunch Item True False

4) A Tea is a Drink True False

5) A Soda is a Tea True False

6) A Hot Dog is a Sandwich True False

7) A Lunch Item is a Sub True False


5
8) A Double Hamburger is a Lunch Item True False Dimla
9.1-Creating Superclasses and
🔙 to divider

to divider
Subclasses

9.1-Practice2

Directions:
1. Write a Class called Pet
2. Write two subclasses called Dog and Cat
3. Create the PetClient and instantiate two objects: dog1 and cat1
4. Compile and run the program.
5. Upload your project to One Drive.

Important Points:
• The keyword extends is used to establish an inheritance relationship between classes.
• A class can extend only one superclass. If a class does not extend another class, its superclass
is Object.
• The Pet class is the superclass (also called parent class or base class);
• The Cat and Dog classes are subclasses (child classes or derived classes) that inherit from
Pet.
• The Pet class defines the attributes and methods common to all pets:
○ name - an instance variable that stores the pet's name
○ getName - a method that returns the name of the pet
○ speak - a method that returns the sound that a pet makes
○ toString - a method that returns a String representation of a Pet object
• The Pet class also provides a constructor that initializes the pet name.
• The Cat and Dog classes inherit the public members of the Pet class.
• Subclasses inherit public class members, not private class members or constructors.
• The members of a class are the fields and methods of a class.
• The fields of a class are the instance variables and the static (class) variables.
• The public members of the Pet class are the public methods getName, speak, and
toString, which the Cat and Dog classes inherit.
• The Pet class's private instance variable, name, is not inherited by Cat and Dog. The Pet
class stores name for its subclasses and gives them access to the private instance variable
through the public method getName.
• The Cat and Dog classes can override any inherited method to change a behavior that its
parent class defined.
• Overriding an inherited method in a subclass occurs when a method in the subclass has the
same signature as a superclass method. The body of the overridden method is modified to
provide specific functionality for the subclass. For Pet subclasses, overriding the speak
method allows the Cat and Dog classes to change how their objects will speak. The
signature of a method is its name and parameter list.
6
Dimla
9.1-Creating Superclasses and
🔙 to divider

to divider
Subclasses

9.1-Practice2

Directions:
1. Write a Class called Pet
2. Write two subclasses called Dog and Cat
3. Create the PetClient and instantiate two objects: dog1 and cat1
4. Compile and run the program.
5. Upload your project to One Drive.

7
Dimla
9.1-Creating Superclasses and
🔙 to divider

to divider
Subclasses

8
Dimla
9.1-Creating Superclasses and
🔙 to divider

to divider
Subclasses

9.1-Practice3

Directions:
1. Write the class headers for each class in the following class hierarchy.

_________________________________________

_________________________________________

_________________________________________

_________________________________________

9
Dimla
9.1-Creating Superclasses and
🔙 to divider

to divider
Subclasses

Solution

extends header

10
Dimla

You might also like