🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

AC-S24-OOP-Lect05-Part1-Classes-Inheritance-Polymorphism.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Lect 05A CST8132 OOP Algonquin College Lect 05A Computer Engineering Technology CST8132 OOP Summer, 2024 Based on resources developed by prof....

Lect 05A CST8132 OOP Algonquin College Lect 05A Computer Engineering Technology CST8132 OOP Summer, 2024 Based on resources developed by prof. Howard Rosemblum, James Mwangi, Anu Thomas and Ramanjeet Singh. Prof. Paulo Sousa Algonquin College Lect 05A Computer Engineering Technology Inheritance / Polymorphism CST8132 OOP Summer, 2024 Based on resources developed by prof. Howard Rosemblum, James Mwangi, Anu Thomas and Ramanjeet Singh. Prof. Paulo Sousa L1 L2 Week 5A: OOP Concepts A2W01 A2W02 A2W09 A2W10 A2W03 A2W11 A2W04 A2W12 A2W05 A2W13 Inheritance A2W06 A2W14 Polymorphism A2W07 A2W15 A2W08 4 Topics Inheritance ‘is-a’ Relationships between Classes Polymorphism existing in ‘many forms’ 5 Warm up – Brief Reviews Q1. 1 minute How does Java implement Encapsulation? List two ways 6 What is Inheritance? Refers to is-a relationship between a super class and subclass Super class (base class, parent class ) - existing top class whose members are inherited. Subclass (child class, derived class) – lower level class inheriting resources from super class Source: Deitel & Deitel 7 What is Inheritance? Reusing robust well-tested classes to build new software Boost programmer productivity (no need to retype) Avoid redundancy in coding. Make software that’s easy to understand and maintain 8 UML Inheritance Example Think of super class GeometricShape. What you see below are all geometric shapes. Circle is-a GeometricShape. Rectangle is-a GeometricShape Two dim Geometric Shapes Circle Square Rectangle Triangle Base Height Radius Length Width Hypotenuse Diameter filled Length color filled color color calcArea() color calcArea() calcArea() calcArea() Perimeter 9 UML Inheritance Example Question: What do they have in common? What is unique to each of them? Three Dim GeometricObject cuboid cube Height Width Length Height Width Length Volume Volume calcSurfaceArea( calcSurfaceArea() ) 10 UML - InheritanceC To Do/Practice Create a program for the class hierarchy shown on the left 11 UML - Inheritance Question: How do we make Circle Inherit from GeometricObject in code? Subclass extends superclass Use the keyword ‘extends’ e.g. class Programmer extends Employee Subclass Superclass is-a public class Circle extends GeometricShape {} The keyword extends informs the compiler that the Circle class is extending its superclass GeometricShape class, to inherit methods getColor, setColor, isFilled, setFilled, and toString. 12 UML - Inheritance Superclass => GeometricShape Subclass => Circle public class Circle extends GeometricShape { import java.util.Date; public class Circle extends GeometricShape { public class GeometricShape { private double radius; public Circle(double radius, String color, private String color = "colourless"; boolean filled) { private boolean filled; this.radius = radius; private Date dateCreated; setColor(color); setFilled(filled); //...constructors... } } //overloaded constructor public GeometricShape(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } //...setters & getters... Superclass fields can only be accessed by } calling superclass setters and getters or via constructor 13 UML - Inheritance Question: How do you make Rectangle to Inherit superclass GeometricShape Subclass Superclass Public class Rectangle extends GeometricShape {} The keyword extends makes Rectangle class to inherit from GeometricShape class, methods getColor, setColor, isFilled, setFilled, and toString 14 Super super() invokes no-arg constructor of its superclass. super(arguments) invokes the superclass constructor that matches the arguments. super() or super(arguments) must be the first statement of the subclass’s constructor - its the only way to explicitly invoke a superclass constructor. Use inheritance thoughtfully It may not apply for some is-a relationships 15 Inheritance Example In Class Demo / Practice 16 Constructor Chaining For an instance of a class to be constructed the compiler invokes constructors of all the superclasses along the inheritance chain. 17 Source: Pearson(Liang) Constructor Chaining Type your What is the output of this code? text 18 Source: Pearson(Liang) Polymorphism Existing in many forms. Idea: Program in the general rather than program in the specific. Writing programs to objects that share the same superclass directly or indirectly as though they are all objects of the superclass. This means that a variable of a supertype can refer to a subtype object. 19 Polymorphism From GeometricShape code 20 Polymorphism Method displayObject() takes a parameter of the GeometricObject type. You can invoke displayObject() by passing any instance of GeometricObject (e.g., new Circle(1, "red", false) and new Rectangle(1, 1, "black", false) in lines 5 and 6). A subclass object can be used wherever its superclass object is used. This is polymorphism (a Greek word meaning “many forms”). In simple terms, polymorphism means that a variable of a supertype can refer to a subtype object. 21 Polymorphism 22 Uses of Polymorphism Provide a way to write programs that process objects that share the same superclass directly or indirectly as though they are all objects of the superclass. 23 Using Scanner A scanner is used to get input from the user. Import Scanner package A scanner should be closed. Offers many methods: next(), nextInt() nextLine(), nextFloat(), nextLine(), nextDouble(). 24 Using Scanner 25 Abstraction A class can implement multiple interfaces, but it can only extend one superclass Source: Pearson/Liang 26 Abstraction Polymorphism enables you to hold a reference to a Chicken object or a Duck object in a variable of type Animal, as in the following code: 27 Remember Modifiers… Access Java UML Within class Within Is Not Modifier keyword symbol package subclass subclass Public public + Y Y Y Y Protected protected # Y Y Y N Package ~ Y Y N N Private private - Y N N N Modifiers are necessary to define proper hierarchy. 28 OOP – Week 5 More About UML 29 Open questions… Any doubts / questions? How we are until now? 30 See you… Enjoy our course and season… 31 OOP – Week 4 Thank you for your attention! Contact: [email protected] 32

Use Quizgecko on...
Browser
Browser