0% found this document useful (0 votes)
8 views

Slaid FP301 OOP 3.4 Apply Inheritance in Java program UPLOAD

Uploaded by

Azrol Hisham
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Slaid FP301 OOP 3.4 Apply Inheritance in Java program UPLOAD

Uploaded by

Azrol Hisham
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

3.

0 OBJECT ORIENTED PROGRAMMING

3.4 Apply Inheritance in Java program


3.4.1 Describe inheritance in Java programs.
3.4.2 Apply keyword super in Java programs.
3.4.3 Explain overriding method in Java programs.
3.4.4 Implement overriding method in Java programs.
3.4.1 Describe inheritance in Java programs..

• Inheritance is the capability of a class to use the properties and


methods of another class while adding its own functionality.

• When we talk about inheritance the most commonly used


keyword would be extends and implements.

• These words would determine whether one object IS-A type of


another.

• By using these keywords we can make one object acquire the


properties of another object.
3
• Java Inheritance defines an is-a relationship between a
superclass and its subclasses.
• IS-A is a way of saying : This object is a type of that object
• classes can be derived from other classes, thereby inheriting
fields and methods from those classes.
• A class that is derived from another class is called a subclass
(also a derived class, extended class, or child class).
• The class from which the subclass is derived is called a
superclass (also a base class or a parent class).

4
• A subclass inherits all the members (fields, methods, and
nested classes) from its superclass.

• Constructors are not members, so they are not inherited by


subclasses, but the constructor of the superclass can be
invoked from the subclass.

• Java uses the extends keyword to set the relationship


between a parent class and a child class

• A subclass inherits all of the public and protected members


of its parent, no matter what package the subclass is in.
5
• A subclass does not inherit the private members of its
parent class

• With use of the extends keyword the subclasses will be able


to inherit all the properties of the superclass except for the
private properties of the superclass.

• The inheritance relationship is transitive: if class x extends


class y, then a class z, which extends class x, will also inherit
from class y.

6
• Consider Student and Teacher as two classes.
• The details of a student are name, age, gender and mark in
different subjects.
• The details of a teacher are name, age, gender, subject
handled and salary.
• To store and manipulate the details of students and
teachers, two classes should be created.
• In one class you store the details of students and in
another class you store the details of teachers as shown in
Figure below

7
class Student{ class Teacher{
String name; String name, subject;
int age; int age;
char gender; char gender;
double mark; double salary;

void getdetails( ){ void getdetails( ) {


----- -----
----- -----
} }
} }

8
4.3.1 Describe inheritance in Java
programs.
class Person{
String name;
int age;
char gender;
}

class Student{ class Teacher{


double mark; String subject;
void getdetails( ) { double salary;
----- void getdetails( ){
----- -----
} -----
} }
}
9
4.3.1 Describe inheritance in Java
programs.
The advantages of Inheritance are:
1. The variables and methods that are common to two or more
classes can be defined in one class and used in other classes.
This is called as code reusability.
2. When there is any modification in the common information, it
will apply to all the inherited classes.
3. Inheritance avoids code redundancy.

There are 3 types of Inheritance. They are:


1. Single Inheritance
2. Multiple Inheritance
3. Multi-Level Inheritance

10
Single Inheritance

Definition: When a class is inherited from only one existing


class, then it is Single inheritance. The inherited class is a
Subclass and the existing class from which a subclass is
created is the super class.
Super class
Class A

Subclass
Class B

11
Multiple Inheritance

Definition: When a class is inherited from more than one


existing class, then it is Multiple inheritance. The inherited
class is a subclass and all the existing classes from which
the subclass is created are super classes.
Class A Class B Super class

Super class

Class C Subclass
Multi-Level Inheritance
Definition: Multi-Level Inheritance is the extension of Single
Inheritance. When a class is inherited from another
subclass, then it is Multi-Level Inheritance.
Class A Super class

Class B
Subclass for Super class for
Class A Class C

Class C
Subclass
A very important fact to remember is that Java only supports only
single inheritance.

This means that a class cannot extend more than one class.

However a class can implement one or more interfaces.

This has made Java get rid of the impossibility of multiple


inheritance
A subclass is created using the extends keyword.

The following is the syntax for creating a subclass:

class <class_name> extends <existing_class_name>{


}

In the above syntax, class is a keyword, class_name is the


subclass name, extends is a keyword and existing_class_name
is the super class name.
Example: How to combine two class using inheritance
3.4.2 Apply keyword super in Java programs
Accessing Superclass Members
Any subclass can invoke super class method using keyword super

If your method overrides one of its superclass's methods, you can


invoke the overridden method through the use of the keyword
Super
Syntax accessing method and variable from superclass:
super.superClassMethod;
super.superClassVariable;
Syntax accessing constructor super class
super();
19
Example: Java Program with inheritance and keyword super
File Superkelas.java
public class Superkelas {
public void statusKelas(){
System.out.println("Dalam Super Kelas");
}
public void method1Superkelas(){
System.out.println("Method Dari Super Kelas");
}
}

20
File SubKelas.java
public class SubKelas extends Superkelas {
public void StatusKelas(){
super.statusKelas();
System.out.println("Dalam Sub Kelas");
}}
File TestSuperSubKelas.java
public class TestSuperSubKelas {
public static void main(String[] args) {
SubKelas s = new SubKelas();
s.StatusKelas();
s.method1Superkelas();
}} 21
3.4.3 Explain overriding method in Java programs

• Method overriding refers to the concept of the


methods in the sub class and the super class using
the same method name with identical signature
(parameters).

• Method overriding can be used in situations, where


you want a method in the super class to be
implemented differently in the sub class but with same
name and parameters.
22
• An instance method in a subclass with the same signature
(name, plus the number and the type of its parameters) and
return type as an instance method in the superclass overrides
the superclass's method.

• If a subclass defines a class method with the same signature as a


class method in the superclass, the method in the subclass hides
the one in the superclass.

• The version of the overridden method that gets invoked is the


one in the subclass.

• The version of the hidden method that gets invoked depends on


whether it is invoked from the superclass or the subclass.

23
• The argument list should be exactly the same as that of the
overridden method.
• The return type should be the same or a subtype of the
return type declared in the original overridden method in
the super class.
• Instance methods can be overridden only if they are
inherited by the subclass.
• A method declared final cannot be overridden.
• A method declared static cannot be overridden but can be
re-declared.
• If a method cannot be inherited then it cannot be
overridden.
24
3.4.4 Implement overriding method in Java programs.
Example Method overiding:
File IlmuHisab.java
public class IlmuHisab {
int no1;
int no2;
int jaw;
void tambah(){
jaw=no1 + no2;
System.out.println("Dalam Superkelas IlmuHisab: Jaw =
"+jaw);
}}
25
File OperasiTambah.java Inheritance

public class OperasiTambah extends IlmuHisab{


void tambah(){ Method overide
jaw=no1+no2;
System.out.println("Dalam Subkelas OperasiTambah: Jaw =
"+jaw);
}}

26
File TestOveriding.java

public class TestOveriding {


public static void main(String[] args) {
OperasiTambah Ot = new OperasiTambah();
IlmuHisab Ih = new IlmuHisab();
Ot.tambah();
Ih.tambah();
}}

27

You might also like