CS Problem Solving and Object Oriented Programming Spring 2019

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Chapter 1 Inheritance University Of Ha’il.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 11: Oct 31-Nov 4, 2011 Aditya Mathur Department of Computer Science Purdue.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Design issues for Object-Oriented Languages
Modern Programming Tools And Techniques-I
Polymorphism.
Lecture 12 Inheritance.
Object-oriented Programming in Java
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Lecture 17: Polymorphism (Part II)
Types of Programming Languages
Object Oriented Programming in Python
Week 8 Lecture -3 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CSC 143 Inheritance.
Chapter 9 Inheritance and Polymorphism
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CMSC 202 Polymorphism.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Method Overriding in Java
Inheritance Cse 3rd year.
Object Oriented Programming
Lecture 18: Polymorphism (Part II)
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 11 Inheritance and Polymorphism Part 1
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CPSC 233 Tutorial 13 March 11/12th, 2015.
Inheritance and Polymorphism
Presentation transcript:

CS 18000 Problem Solving and Object Oriented Programming Spring 2019 Section LE2 Week 12: Lecture 25, April 8. 2019 Slides updated: 3:32pm April 10, 2019 Aditya Mathur Professor, Department of Computer Science Purdue University West Lafayette, IN, USA https://www.cs.purdue.edu/homes/apm/courses/CS180_Java/CS180Spring2019/

©Aditya Mathur. CS 180. Fall 2019.Week 12 Today Polymorphism: Review: Inheritance Static Method signature Dynamic 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Inheritance A common feature of OO languages Enables code reuse Example: The JButton class java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent javax.swing.AbstractButton javax.swing.JButton toString(), equals() Inherits from Can be used by a JButton object add() 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Polymorphism Poly: Many Morph: “Change smoothly from one image to another by small gradual steps using computer animation techniques” Polymorphism: The condition of occurring in several different forms. Source: Oxford living Dictionary 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Inheritance: Questions Can a child class re-use methods in parent class? Can a child class re-define methods already in the parent class? Can a child class re-define instance variables already in the parent class? Can an object of a child class be “upcast” to an object of parent class? Can an object of a parent class be “downcast” to an object of child class? Can a method signature be changed in a class and a different method with the same name be defined Let us answer these questions using the notion of “polymorphism” in Java. 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Polymorphism in Programming Languages Polymorphism: A feature of a programming language that allows routines to use variables of different types at different times. Static polymorphism Compiler can determine, at compile time, which method is called. Dynamic polymorphism Method call is determined at run time. 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Static Polymorphism: Method overloading public class Pizza { String type; ArrayList<String> toppings=new ArrayList<String>(); Pizza(){ type="Cheese";} Pizza(String type){this.type=type;} Pizza(String type, ArrayList<String> t){ this.type=type; toppings=t; } public void print(){ System.out.println("Pizza ordered: "+type); if (toppings.size()==0){ System.out.println("Toppings: None "); }else{ System.out.println("Toppings: "+toppings); } } }// End of class Pizza Method with the same name appears multiple times but with different signatures. 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Static Polymorphism: Method overloading import java.util.*; public class StaticPoly { public static void main(String [] args){ ArrayList<String> toppings = new ArrayList<String>(); toppings.add("Mushroom"); toppings.add("Pineapple"); toppings.add("Black OLives"); Pizza p1 = new Pizza(); Pizza p2 = new Pizza("Pepperoni"); Pizza p3 = new Pizza("Pepperoni", toppings); p1.print(); p2.print(); p3.print(); } }// End of class 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Method signature Method name Number of parameters Order of parameters Return type and exceptions not considered in method signature. 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Dynamic Polymorphism: Method overriding Redefine a method from the parent class. Use @Override to indicate that a method from parent class is being overridden. import java.util.*; public class PizzaJazz extends Pizza { PizzaJazz(String type, ArrayList<String> t) { this.type = type; toppings = t; } @Override public void print(){ System.out.println("Eating pizza makes you intelligent."); super.print(); } } Pizza p3 = new PizzaJazz("Pepperoni", toppings); p3.print(); 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Upcasting Casting an object of a sub-class to its super-class. This is allowed in Java. 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Upcasting Example public class Pizza { Pizza(){ System.out.println("Pizza created."); } public void eat(Pizza p){ System.out.println("Eating generic pizza."); } } public class Pepperoni extends Pizza{ public void eat(){ System.out.println("Eating pepperoni Pizza."); } } public class Cast { public static void main(String[] args){ Pepperoni p=new Pepperoni(); p.eat(); Pizza q= (Pizza)p; q.eat(p); } } 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Downcasting Casting an object of a super-class to its sub-class. Not allowed unless the object being downcast was created using a subclass. PizzaJazz is child of Pizza Pizza p3 =PizzaJazz("Pepperoni", toppings); PizzaJazz p4=(Pizza)p3; Not allowed 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 instanceof() Enables to check if object a is an instance of class B. if (a instanceof B){ System.out.println(“a is an instance of Class B”); else{ System.out.println(“a is not an instance of Class B”); 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Live demo 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Create Your Own Pizza Pizza [Generic] Pepperoni Mushroom Toppings addToppings () removeToppings () 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12

©Aditya Mathur. CS 180. Fall 2019.Week 12 Quiz: 04/10/2019 04/10/2019 ©Aditya Mathur. CS 180. Fall 2019.Week 12