• Tidak ada hasil yang ditemukan

Rules for Java Method Overriding

N/A
N/A
Protected

Academic year: 2024

Membagikan "Rules for Java Method Overriding"

Copied!
23
0
0

Teks penuh

(1)

6001104-3 Structured Programming

L. Rafika Maaroufi

(2)

Chapter 7: Endomorphism,

Abstract & Interface in Java

(3)

Outline

Polymorphism Abstract

Interface

(4)

Polymorphism in java

Polymorphism comes from Greek meaning “many forms.”

Polymorphism is one of the OOPs feature that allows us to perform a single action in different ways.

In the context of object oriented design, polymorphism refers to the ability of a reference variable to take different forms.

In particular, polymorphism enables us to write programs that process objects that share the same superclass in a class

hierarchy as if they are all objects of the superclass.

(5)

Polymorphism in java

polymorphism allows you define one interface and have multiple implementations.

There are two types of polymorphism in java:

Runtime polymorphism (Static Polymorphism)

Compile time polymorphism (Dynamic Polymorphism)

(6)

Compile time Polymorphism

Polymorphism that is resolved during compiler time is known as static polymorphism.

Example:

Method overloading

Method overloading:

is a feature that allows a class to have more than one

method having the same name, if their argument lists are different

(7)

Example of Compile time Polymorphism

T

(8)

Runtime Polymorphism

It is also known as Dynamic Polymorphism

is a process in which a call to an overridden method is resolved at runtime.

Example:

Method Overriding

Method Overriding:

Declaring a method in sub class which is already present in parent class

Overriding is done so that a child class can give its own implementation to a method which is already provided by the parent class

(9)

Runtime Polymorphism

Rules for Java Method Overriding:

The method must have the same name as in the parent class

The method must have the same parameter as in the parent class.

There must be an IS-A relationship (inheritance).

(10)

Example of Runtime Polymorphism (1/3)

Consider a scenario where Bank is a class that provides

functionality to get the rate of interest. However, the rate of interest varies according to banks.

For example, SBI, ICICI and AXIS banks could provide 8%, 7%, and 9% rate of interest.

(11)

Example of Runtime Polymorphism (2/3)

T

(12)

Example of Runtime Polymorphism (3/3)

T

(13)

Abstract Classes

Abstract class: a class that is declared using “abstract”

keyword.

It can have abstract methods(methods without body) An abstract class can not be instantiated, which means you are not allowed to create an object of it.

A class has an abstract method, it must be declared class

abstract.

(14)

Abstract class declaration

//Declaration using abstract keyword abstract class A{

//This is abstract method abstract void myMethod();

//This is concrete method with body void anotherMethod(){

//Does something }

}

(15)

Basics and example of abstract method.

1)

Abstract method has no body.

2) Always end the declaration with a semicolon(;).

3) It must be overridden. An abstract class must be extended and in a same way abstract method must be overridden.

(16)

Why we need an abstract class?

Example:

We have a class Animal that has a method sound() and the subclasses of it like Dog, Lion, Horse, Cat etc.

Since the animal sound differs from one animal to another, there is no point to implement this method in parent class.

This is because every child class must override this method to give its own implementation details, like Lion class will say “Roar” in this method and Dog class will say “Woof”.

(17)

Why we need an abstract class? (cont.)

(18)

Interface in java

Abstract class is used for achieving partial abstraction An interface is used for full abstraction

Interface looks like a class but it is not a class.

An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body).

The variables declared in an interface are public, static & final by default

(19)

Interface in java (cont.)

Interface cannot be declared as private, protected or transient.

All the interface methods are by default abstract and public.

While providing implementation in class of any method of an interface, it needs to be mentioned as public.

implements keyword is used by classes to implement an interface.

(20)

The use of interface in Java

The interface is used for full abstraction

Java programming language does not allow you to extend

more than one class, However you can implement more than one interfaces in your class.

In java, multiple inheritance is not allowed, however you can use interface to make use of it as you can implement more than one interface.

The class that implements interface must implement all the methods of that interface

(21)

Interface declaration in java (cont.)

Interfaces are declared by specifying a keyword “interface”.

Syntax :

interface MyInterface {

//All the methods are public abstract by default //As you see they have no body

public void method1();

public void method2();

}

(22)

Example

T

(23)

References

Java: How to Program, 9e, Dietel and Dietel, Pearson 0273759760

Data Structures and Algorithms in Java, 6th edition, by M.T. Goodrich and R.

Tamassia. John Wiley and Sons, Inc., ISBN:

0-471-73884-0

Java The Complete Reference Ninth Edition

Referensi

Dokumen terkait

Like I said before, I have only had my HydroFloss for a week, but I like the results I’m seeing so far.. When finished using it, I feel like my gums have just had a good massage and

Add member variables and event handlers to the view class to modify the document variables when you drag with the left mouse button just like we did in the last example (hint:

Static members are variables, methods, or nested classes, the last one will be explained later in this book. All objects have their own copy of the instance variables, but for

What you need to know Structure of a class definition Instance variables Constructors Instance methods toString method private instance variables Define constructors Define

It offers an interface just like that provided in thehelvet package for the Helvetica Font used as the Sans Serif Font but with a different default magnifi- cation.. 1 Introduction

Consider the following example : class Animal{ public void move{ System.out.println"Animals can move"; } } class Dog extends Animal{ public void move{

Keyword What It Does abstract Indicates that the details of a class, a method, or an interface are given elsewhere in the code.. assert Tests the truth of a condition that the

Method Overloading  Method overloading occur when methods or constructors of the same name declared in the same class  Methods overloading is used to create several methods with the