module-6 (2)
module-6 (2)
Inheritance in Java
Learning Outcomes:
only once and they can be used by all subclasses. A subclass only needs to
implement the differences between itself and the parent.
In real life scenario, a child inherits the properties from his parents. A
similar concept is followed in Inheritance in Java, where it has two classes:
class mother
{
// body of the program
}
class daughter extends mother
{
// body of the program
}
Where:
class mother – is the parent class
class daughter – is the child class
extends – is the keyword used to derive a class and inherits the
properties and methods of the parent class.
{
int total;
}
}
1. SINGLE INHERITANCE
Where the class animal is the parent class with a method name eat, while the
class dog is a child class which extends the parent class animal. Inside the main
method, an object name browny was created to implicitly invoked superclass or
parent class.
eating
3. HIERARCHICAL INHERITANCE
public class A
{
CLASS A …………
}
public class B extends A
{
……………
}
CLASS B CLASS C
public class C extends A
{
……..
}
Hierarchical inheritance refers to a child and parent class
relationship where more than one classes extends the same class. For
example, classes B and C extends the same class A.
* NOTE: In real life scenario, the hierarchical inheritance is same with parent and
children(siblings) relationship where their parent is class A while the class B and class C
are the children (class B and class C are siblings).
Take a look at sample code below.
1 class Animal
2 {
3 void eat()
4 {
5 System.out.println(“eating…”);
6 }
7 }
8 class Dog extends Animal
9 {
10 void bark()
11 {
12 System.out.println(“barking…”);
13 }
14 }
15 class Cat extends Animal
16 {
17 void meow()
18 {
19 System.out.println(“meowing…”);
20 }
21
22 public static void main(String args[])
23 {
24 Cat cathy=new Cat();
25 cathy.meow();
26 cathy.eat();
Object-Oriented Programming
27 }
28 }
Thinking Box:
What will be the output of the sample program if line 25 is
removed?
4. MULTIPLE INHERITANCE
public class A
CLASS A CLASS B {
…………
}
public class B
{
…………
CLASS C }
public class C extends A, B
Java does not support multiple {
inheritance. …..
}
1.
class Base {
public void show() {
System.out.println("Base::show() called");
}
}
30 addition.num();
31 System.out.println("the sum is:"+addition.sum());
32
33 product multiply= new product();
34 multiply.num();
35 System.out.println("the product
36 is:"+multiply.prod());
37 System.out.println("the sum is:"+multiply.sum());
38
39 quotient divide= new quotient();
40 divide.num();
41 System.out.println("the sum is:"+divide.sum());
42 System.out.println("the quotient is:"+divide.quot());
43 System.out.println("the product is:"+divide.prod());
44 }
45 }
46
47 class product extends arithmetic
48 {
49 int prod()
50 {
51 return one*two;
52 }
53 }
54
55 class quotient extends product
56 {
57 int quot()
58 {
59 return one/two;
}
}
C.
Thinking Box:
What will be the output of the sample program if the method
num is removed?
To supplement the lesson in this module, you may visit the following
links:
http://iiti.ac.in/people/~tanimad/JavaTheCompleteReference.pdf
http://www.java.sun.com
www.Callingallinnovators.com
ASSESSMENT TASK
class Base
{
public void Print()
{
System.out.println("parent");
}
}
class Derived extends Base
{
public void Print()
{
System.out.println("child");
}
}
class Main
{
public static void DoPrint( Base o )
{
o.Print();
}
public static void main(String[] args)
{
Base a = new Base();
Base b = new Derived();
Derived c = new Derived();
DoPrint(a);
DoPrint(b);
DoPrint(c);
}
}
2.
class A
{
{
System.out.println(1);
}
}
class B extends A
{
{
System.out.println(2);
}
}
class C extends B
Object-Oriented Programming
{
{
System.out.println(3);
}
}
3. Determine the output of the program below. Note that fun() is public
Class Parent
in{ base and private in derived.
public void Anak()
{
System.out.println("Base");
}
}
class Child extends Parent
{
private void Anak()
{
System.out.println("Derived");
}
}
}
public class health extends Benefit
{
private int coverageOption;
private float employeePremium;
}
First Class
Write a program that will display the employee information such as Full
name, date of birth, age, gender and position.
Second class
Create a program that will compute the tax of five (5) individual and
display their net income. The computation of the tax is based on the
following:
The input shall be the following: civil status code, dependents code (0 – without
dependents / 1 – with dependents), and gross income.
Note: There must be separate methods for the employee information, computation
of tax, pagibig and SSS
: Write a member function to get the employee information in the first class
Third class
Create a program that will convert a Philippine currency. The program will
assume the following rate of exchange per Philippine Peso. In the case of
other types of currency not listed, the program will just display an error
message.
D Dollar 44.50
Object-Oriented Programming
Y Yen 0.58
R Riyal 18.43
P Pound 115.65
Note: Please referto the sample below.
Money Changer
Amount 5000
Currency D
Converted 222,500
Note: The net income of the employee in the second class will serves an input in
rate of exchange of Philippine(amount) peso to other currency.
: Write a member function to display the converted peso to currency for
respective salary of the employees.
References
Agarwal , S. & Bansal, H., 2018. Java in Depth. First Edition. ISBN:
9789386551573