Polymorphism Session 4 PDF
Document Details
BINUS University
2020
Tags
Summary
This document is a lecture presentation on Polymorphism, specifically session 4, and it's contents are on object-oriented programming in Java. It covers learning outcomes, types of polymorphism (static and dynamic), examples, and explanations. The presentation is from BINUS University, and the effective period is July 2020.
Full Transcript
Course : Course Name Effective Period : July 2020 Polymorphism Session 4 Learning Outcomes LO 1 EXPLAIN Object Oriented concept LO 2 SOLVE the algorithm problem using Object Oriented concept LO 3 CONSTRUCT a simple application with Object Oriented concept ...
Course : Course Name Effective Period : July 2020 Polymorphism Session 4 Learning Outcomes LO 1 EXPLAIN Object Oriented concept LO 2 SOLVE the algorithm problem using Object Oriented concept LO 3 CONSTRUCT a simple application with Object Oriented concept Contents Outline Introduction Types of polymorphism Implementing Polymorphism Casting object The instanceof Operator The ArrayList Class Introduction What is Polymorphism and why do we need that? Polymorphism means many forms / shapes It is one of the 3 pillars of OOP besides encapsulation and inheritance Efficient and less redundancy Types of Polymorphism 1) Static Polymorphism This can be achieved by using overloading method Overloading means there are several methods present in a class having the same name but different types / order / number of parameters It is known as well as compile time polymorphism or static binding At compile time, Java known which method to invoke by checking the method signatures Static Polymorphism Example public class Calculator{ public int add(int x, int y){ return x+y; } public int add(int x, int y, int z){ return x+y+z; } public double add(double x, double y){ return x+y; } public double add(double x, double y, double z){ return x+y+z; } } Types of Polymorphism 2) Dynamic Polymorphism This can be achieved by 1) overriding a particular method of the superclass 2) Implementing Abstract 3) Implementing Interface As the method to call is determined at runtime It is known as well as dynamic binding Dynamic Polymorphism (1) Example – Overriding a particular method of the superclass public class Vehicle{ public void move(){ System.out.println("Vehicle can move!"); } } class MotorBike extends Vehicle{ public void move(){ System.out.println("Motorbike can move!"); } } Dynamic Polymorphism (2) Abstract Class Declare general characteristic of a subclass Declared abstractly Could not create an object using operator new Only use as a superclass from other classes and form in abstract Declared using keyword abstract Superclass have to be real so it can be abstracted to subclass Dynamic Polymorphism (3) Abstract Class A template of design for subclass Provides an abstract funcstion too (abstract class) Function is overridden in subclass An object should be able to implement all abstract method in an abstract class Dynamic Polymorphism (4) Example – Implementing Abstract Class abstract class Shape{ //Declaration of abstract class private int x,y; public abstract double computeArea(); //Declaration of abstract method } Dynamic Polymorphism (5) Example – Implementing Abstract Class class Rectangle extends Shape{ Class Rectangle is private double width = 12; private double height = 20; inheritance of abstract public double computeArea(){ class Shape (using return width * height; } extends keyword) } computeArea() class Circle extends Shape{ function that private double radius = 20; public double computeArea(){ overridden from its return Math.PI * radius * superclass radius; } The same happened } with Circle class Dynamic Polymorphism (6) Example – Interface Class Consist only of constants and abstract methods Can not make an object with the new operator Create a subclass that has more than one superclass (Multiple inheritance solution) Not inherited but implemented Declared using keyword interface In subclass using keyword implements Dynamic Polymorphism (7) Example – Interface Class Each interface is compiled into bytecode lines, just like regular class All method declared in interface should be overridden by implementing class Dynamic Polymorphism (8) Example – Implementing Interface Class interface Animal { //Declare interface class public void eat(); //Abstract method public void travel(); //Abstract method } public class Mammal implements Animal { //Using implement if you want // to use interface class Shape public void eat() { //Function in interface class Animal // should be overridden in subclass point System.out.println("Mammal eats"); } public void travel() { //Function in interface class Animal // should be overridden in subclass point System.out.println("Mammal travels"); } Interface VS Abstract Class Casting Object (1) Converting an object from a class to other type class in an inheritance hierarchy There are 2 kind of Casting Object : 1. Implicit Casting Object obj = new MountainBike(); 2. Explicit Casting MountainBike myBike = (MountainBike) obj; Error if: Student b = o; Because a student object is an instance of Object, but an Object is not necessarily an instance of Student. Casting Object (2) To ensure that the object is an instance of another object before attempting a casting Object myObject = new Circle(); //Peform casting if myObject is an instance of Circle if (myObject instanceof Circle){ system.out.println("This is an instance of Circle") } ArrayList To construct ArrayList : ArrayList cityList = new ArrayList(); To add String in the object list: cityList.add("Aachen"); cityList.add("Dresden"); cityList.add("Hamburg"); To remove a specific String in the object list: cityList.remove("Dresden"); To remove the cityList by specifying the index: cityList.remove(1); Reference Introduction to Javaa Programming. 10ed. Liang. Chapter 11 https://www.sitepoint.com/quick-guide-to-polymor phism-in-java/ http://www3.ntu.edu.sg/home/ehchua/programmin g/java/j3b_oopinheritancepolymorphism.html https://docs.oracle.com/javase/tutorial/java/IandI/s ubclasses.html Bina Nusantara