You are on page 1of 10

SHS

TVL - ICT (Computer Programming -


Java)
Activity Sheet Quarter 3 – C1
Work with Inheritance

REGION VI – WESTERN VISAYAS


TVL SHS – ICT (Computer Programming-Java)
Activity Sheet No. 1
First Edition, 2020

Published in the Philippines


By the Department of Education
Region 6 – Western Visayas

Republic Act 8293, section 176 states that: No copyright shall subsist in any
work of the Government of the Philippines. However, prior approval of the
government agency or office wherein the work is created shall be necessary for
exploitation of such work for profit. Such agency or office may, among other things,
impose as a condition the payment of royalties.

This Learning Activity Sheet is developed by DepEd Region 6 – Western


Visayas.

ALL RIGHTS RESERVED. No part of this learning resource may be


reproduced or transmitted in any form or by any means electronic or mechanical
without written permission from the DepEd Regional Office 6 – Western Visayas.

Development Team of TVL SHS – ICT (Computer Programming-Java)


Activity Sheet

Writer: Ma. Louella B. Gavan

Content/Language Validator: Jayson Gillesania

Layout Artist:

Schools Division Quality Assurance Team:


Schubert Anthony C. Sialongo
Edward E. Baña
Allan B. Montenegro
Michelle P. Jordan
Division of Antique Management Team:
Felisa B. Beriong, CESO VI
Corazon C. Tingson
Gaudencio C. Riego, PhD
Schubert Anthony C. Sialongo
Edward E. Baña

Regional Management Team


Ma. Gemma M. Ledesma
Josilyn S. Solana
Elena P. Gonzaga
Donald T. Genine
April C. Velez
Arnold P. Mahinay
Introductory Message
Welcome to TVL SHS – ICT (Computer Programming-Java)!

The Learning Activity Sheet is a product of the collaborative efforts of the


Schools Division of Antique and DepEd Regional Office VI - Western Visayas
through the Curriculum and Learning Management Division (CLMD). This is
developed to guide the learning facilitators (teachers, parents and responsible
adults) in helping the learners meet the standards set by the K to 12 Basic Education
Curriculum.

The Learning Activity Sheet is self-directed instructional materials aimed to


guide the learners in accomplishing activities at their own pace and time using the
contextualized resources in the community. This will also assist the learners in
acquiring the lifelong learning skills, knowledge and attitudes for productivity and
employment.

For learning facilitator:

The TVL SHS – ICT (Computer Programming-Java) Activity Sheet will


help you facilitate the leaching-learning activities specified in each Most Essential
Learning Competency (MELC) with minimal or no face-to-face encounter between
you and learner. This will be made available to the learners with the references/links
to ease the independent learning.

For the learner:

The TVL SHS – ICT (Computer Programming-Java) Activity Sheet is


developed to help you continue learning even if you are not in school. This learning
material provides you with meaningful and engaging activities for independent
learning. Being an active learner, carefully read and understand the instructions then
perform the activities and answer the assessments. This will be returned to your
facilitator on the agreed schedule.
Quarter 3, Week 1
Learning Activity Sheets (LAS) No. 1

Name of Learner:________________________________________________________
Grade and Section:___________________________________Date: ______________

TVL SHS – ICT (Computer Programming-Java) ACTIVITY SHEET


Apply basics of Java Language

I. Learning Competency with Code


Work with inheritance and handling exceptions- TLE_ICTJAVA11-12POAD-IIj-IIIa-c-30
a. Implement inheritance in accordance with Java framework.

II. Background Information for Learners

In Object-oriented Programming, computer programs are designed in such a way


everything is an object that interacts with one another. Inheritance is an integral part of Java
OOPs which lets the properties of one class to be inherited by the other. It basically, helps in
reusing the code and establish a relationship between different classes.  
In Java, inheritance refers to a feature of object-oriented programming that creates
classes that are derived from other classes. A class that is based on another class is said to
inherit the other class.
Inheritance is one of the core concepts of object-oriented programming (OOP)
languages. It is a mechanism where you can derive a class from another class for a hierarchy
of classes that share a set of attributes and methods.

Inheritance Hierarchy
In Java, the most important aspect of inheritance is that a class that is derived from a
base class can in turn be used as the base class of another derived class. That class is called a
superclass, or parent class. The derived class is called subclass, or child class. The subclass is
automatically given all the methods and fields defined by its superclass. A derived class can add
features to the base class it inherits bedefining its own methods and fields. This is one way a
derived class distinguishes itself form its base class.
As we know, a child inherits the properties from his parents. A similar concept is followed
in Java, where we have two classes:
1. Parent class (Super or Base class)
2. Child class (Subclass or Derived class)
A class which inherits the properties is known as Child Class whereas a class whose
properties are inherited is known as Parent class.
You use the keyword extends to identify the class that your subclass extends. If you
don’t declare a superclass, your class implicitly extends the class Object. Object is the root of all
inheritance hierarchies; it’s the only class in Java that doesn’t extend another class.

Object

Class A Class D

Class B Class C
Figure 1: Class Hierarchy in Java
Syntax: Inheritance in Java
To inherit a class, we use extends keyword. Here class XYZ is child class and class
ABC is parent class. The class XYZ is inheriting the properties and methods of ABC class.

class XYZ extendsABC


{
}

Inheritance Example
The base class Teacher and a sub class PhysicsTeacher. Since
class PhysicsTeacherextends the designation and college properties and work() method from
base class, we need not to declare these properties and method in sub class.

Here we have collegeName, designation and work() method which are common to all
the teachers so we have declared them in the base class, this way the child classes
like MathTeacher, MusicTeacher and PhysicsTeacher do not need to write this code and can be
used directly from base class.

Sample Code:
classTeacher {
String designation = "Teacher";
String collegeName = "Beginnersbook";
voiddoes(){
System.out.println("Teaching");
}
}
publicclassPhysicsTeacherextendsTeacher{
StringmainSubject = "Physics";
publicstaticvoidmain(Stringargs[]){
PhysicsTeacher obj = newPhysicsTeacher();
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does();
}
}
Output:
Beginnersbook
Teacher
Physics
Teaching

Note:
The derived class inherits all the members and methods that are declared as public or
protected. If the members or methods of super class are declared as private then the derived
class cannot use them directly. The private members can be accessed only in its own class.
Such private members can only be accessed using public or protected getter and setter
methods of super class as shown in the example that follows.
classTeacher{
privateString designation ="Teacher";
privateStringcollegeName="Beginnersbook";
publicStringgetDesignation(){
returndesignation;
}
protectedvoidsetDesignation(String designation){
this.designation= designation;
}
protectedStringgetCollegeName(){
returncollegeName;
}
protectedvoidsetCollegeName(StringcollegeName){
this.collegeName=collegeName;
}
voiddoes(){
System.out.println("Teaching");
}
}

publicclassJavaExampleextendsTeacher{
StringmainSubject="Physics";
publicstaticvoidmain(Stringargs[]){
JavaExample obj =newJavaExample();
/* Note: we are not accessing the data members
* directly we are using public getter method
* to access the private members of parent class
*/
System.out.println(obj.getCollegeName());
System.out.println(obj.getDesignation());
System.out.println(obj.mainSubject);
obj.does();
}
}

The output is:


Beginnersbook
Teacher
Physics
Teaching

The important point to note in the above example is that the child class is able to access
the private members of parent class through protected methods of parent class. When we make
an instance variable(data member) or method protected, this means that they are accessible
only in the class itself and in child class. These public, protected, private etc. are all access
specifiers and we will discuss them in the coming tutorials.

Types of inheritance
The following are the types of Inheritance in Java:
1. Single Inheritance: refers to a child and parent class relationship where a class extends
the another class.

Figure 2: Single Inheritance

2. Multilevel inheritance: refers to a child and parent class relationship where a class
extends the child class. For example, class C extends class B and class B extends class
A.
A

Figure 3: Multilevel Inheritance

3. Hierarchical inheritance: refers to a child and parent class relationship where more
than one classes extends the same class. For example, classes B, C & D extends the
same class A.
A

B C D

Figure 4: Hierarchical Inheritance


4. Multiple Inheritance: refers to the concept of one class extending more than one
classes, which means a child class has two parent classes. For example, class C
extends both classes A and B. Java doesn’t support multiple inheritance, read more
about it here.

A B

Figure 5: Multiple Inheritance

5. Hybrid inheritance: Combination of more than one types of inheritance in a single


program. For example class A & B extends class C and another class D extends class A
then this is a hybrid inheritance example because it is a combination of single and
hierarchical inheritance.

Inheritance and Method Overriding


When we declare the same method in child class which is already present in the parent
class this is called method overriding. In this case, when we call the method from child class
object, the child class version of the method is called. However, we can call the parent class
method using super keyword as I have shown in the example below:

classParentClass{
//Parent class constructor
ParentClass(){
System.out.println("Constructor of Parent");
}
voiddisp(){
System.out.println("Parent Method");
}
}
classJavaExampleextendsParentClass{
JavaExample(){
System.out.println("Constructor of Child");
}
voiddisp(){
System.out.println("Child Method");
//Calling the disp() method of parent class
super.disp();
}
publicstaticvoidmain(Stringargs[]){
//Creating the object of child class
JavaExample obj =newJavaExample();
obj.disp();
}
}

The output is:


Constructor of Parent
Constructor of Child
ChildMethod
ParentMethod

III. Accompanying DepEd Textbook and Educational Sites

Inheritance in Java Programming with examples. (n.d.). Retrieved February 9, 2021, from
www.beginnersbook.com: https://beginnersbook.com/2013/03/inheritance-in-java/
Janssen, T. (2017, December 14). OOP Concept for Beginners: What is Inheritance? Retrieved
February 8, 2021, from www.stackify.com: https://stackify.com/oop-concept-inheritance/
Vaidya, N. (2020, May 20). Inheritance in Java – Mastering OOP Concepts. Retrieved January
8, 2021, from www.edureka.co:
https://www.edureka.co/blog/inheritance-in-java/#Inheritance

IV. Activity Proper

a. Exercises / Activities

1. Consider the following two classes:

public class ClassA {


public void methodOne(int i) {
}
public void methodTwo(int i) {
}
public static void methodThree(int i) {
}
public static void methodFour(int i) {
}
}

public class ClassB extends ClassA {


public static void methodOne(int i) {
}
public void methodTwo(int i) {
}
public void methodThree(int i) {
}
public static void methodFour(int i) {
}
}

2. Guide Questions.
a. What is inheritance?
b. How does overriding occur?
c. Which method overrides a method in the superclass? Why?
d. Which method hides a method in the superclass? Why?
e. What do the other methods do?

3. Using your mobile phone or computer (if available), open the browser (Mozilla
Firefox or google chrome) and open the link provided. Watch on YouTube on how
to write inheritance program in javafor further understanding. Here’s the link:
https://www.youtube.com/watch?v=zbVAU7lK25Q

V. Reflection
a. As an ICT Programming student, how can this lesson help you become a
successful programmer?
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________

You might also like