AC-S24-OOP-Lect03-Part1-Inheritance.pdf

Full Transcript

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

Lect 03A CST8132 OOP Algonquin College Lect 03A 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 03A Computer Engineering Technology Inheritance 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 3: Inheritance A2W01 A2W02 A2W09 A2W10 A2W03 A2W11 A2W04 A2W12 A2W05 A2W13 Basic concepts A2W06 A2W14 Examples A2W07 A2W15 A2W08 4 OOP – Week 3 Basic Concepts 5 Objectives Features of OO Encapsulation Polymorphism Abstraction Inheritance Package 6 Objects, Attributes, and Behaviors of Objects Procedural Concepts programming Write methods or Object: procedures that perform An entity in real-life with distinct states operations on data. (attributes) and behaviors (methods). (focus of CST8116) Examples of real world Objects: student, Object Oriented patient, librarian, rectangle, invoice, account Programming Has a unique identity, state, and behavior. Writing reusable programs using objects that encapsulate data and methods. (Focus of CST8132) 5 7 Objects, Attributes, and Behaviors of Objects State of an object: its properties/attributes represented by data fields e.g. rectangle => width, length Behavior of object: its actions defined by methods e.g. All are circles invoking a method is asking an object to perform an action Belong to class e.g. invoke getArea() asks a rectangle object to multiply L * Circle each W and return its area. got a radius but o different Class: Blueprint or / Template represents Objects of the size same type. Jog your mind: What’s the Similarities and Differences? 8 Objects, Attributes, and Behaviors of Objects Class: a template for creating objects Terminology In class we talked about an architectural drawing that is used to create/build similar house objects in different neighborhoods. 9 Objects, Attributes, and Behaviors of Objects UML Class and Objects example: product1 and product2 are created out of class Product NB: This is not inheritance This is instantiation 10 Objects, Attributes, and Behaviors of Objects Class Terminology Think of a class as a construct to define objects of the same type. Accomplishes this using a special method type called a constructor. Constructors Creates a new object Initializes data fields Default constructor No-arg constructor (no args) Parameterized constructor (has args) 11 Objects, Attributes, and Behaviors of Objects Class - a construct that defines objects of same type Take note of UML Notation for constructor and Method More on this later 12 Class - a construct that defines objects of same type A Constructor initializes a new circle when invoked in the main class Recall class student we implemented in class. 13 Class Definition Test Class – contains main () method – the ignition key 14 Source: Pearson (Liang) Encapsulation Source: Deitel & Deitel A way to sure that "sensitive" data is hidden from external users. To achieved through: Declaring class variables/attributes as private Mutators provide public get and set methods to access /setters and update the value of a private variable 15 Specify accessibility Packages & Visibility Modifiers scope for classes, methods and properties Default modifier Specify visibility or accessibility scope of a class (Package-private or and its data fields and methods package access) : accessible in same package. Public modifier: accessible from any c Private modifier: accessible only within host class. Protected modifier 16 Packages & Visibility Modifiers packagename.classname.methodname ‘public’ modifier is not used. C1 is package-private and therefore cannot be accessed by C3 in package p2. 17 Association – Aggregation and Composition (Deitel & Deitel Chapter 8, 9) Association - Relationship by two classes through their objects. student1 ▪ One-one ▪ One-to-many program student2 ▪ Many-to-many Course3 - Implements Java’s multiple inheritance 18 Association – Aggregation and Composition (Deitel & Deitel Chapter 8, 9) Two Forms of Association Composition Aggregation A class references objects of other May be a has-a relationship e.g. a classes as its members. house has a door Has-a relationship May be a part-of relationship e.g. a car has an engine. one won’t function Unidirectional – one way: student has a in the absence of the other. program 19 Composition: Example A class references objects of other classes as its members. Composition – strong/strict association Aggregation: weak association Player can exist independent of Team. When a car object is created it automatically Car must have an engine to function. When references the engine object a car object is created it automatically Source: Tutorialspoint 20 references the engine object Association - summary Aggregation Composition Dependency: child object child object can exist can exist independently of the independently of the parent object parent object Bank & Employee Human & heart Type of has-a part-of relationship Type of weak strong association 21 Composition Example 22 Composition Example Employee classes EmployeeTest class When Employee object is created it automatically references and uses the Date object 20 23 Inheritance A form of is-a relationship. Programmer is an employee, salesrep is an employee, product manager is an employee Programmer Salesrep Productmanage Hourlyrate salary commission 21 24 Inheritance Book reading chapter 9 Deitel & Deitel (CSI see for details) Objects created form subclass have own unique characteristics but they also inherit properties of superclass. Triangle has height is a two- dim shape but has area. Circle has radius is a two-dim shape but has area, 25 UML - Inheritance Subclass extends superclass e.g. Class Programmer extends Employee 22 Source: Pearson 26 Do it yourself super() invokes no-arg Look up the use of keyword constructor of its superclass. super(arguments) invokes super the superclass constructor that matches the arguments. in Inheritance and write a simple working program super() or super (arguments) must be the first statement of the subclass’s constructor its the only way to explicitly invoke a superclass constructor. 27 The Keyword super in Inheritance Replac ed with See you… Enjoy our course and season… 29 Open questions… Any doubts / questions? How we are until now? 30 OOP – Week 3 Thank you for your attention! Contact: [email protected] 31

Use Quizgecko on...
Browser
Browser