Advanced Object-Oriented Programming

Slides:



Advertisements
Similar presentations
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Advertisements

OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
HST 952 Computing for Biomedical Scientists Lecture 2.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
LECTURE 07 Programming using C# Inheritance
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Chapter 8 More Object Concepts
What is inheritance? It is the ability to create a new class from an existing class.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Object-Oriented Design Justin Callanan CS 597. Object-Oriented Basics ● Data is stored and processed in “objects” ● Objects represent generalized real.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
CIS224 Software Projects: Software Engineering and Research Methods Lecture 1b Object concepts (Based on Stevens and Pooley (2006), Chapter 2) David Meredith.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
Welcome back!. Object Oriented Programming – Encapsulation Classes encapsulate state (fields) and behavior (methods) – Polymorphism Signature Polymorphism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Coming up Which methods are where? – Overriding Calling super’s methods Coupling and cohesion.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Objects Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Programming in Java: lecture 7
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Inheritance ITI1121 Nour El Kadri.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Python First Edition STARTING OUT WITH Chapter 10 Inheritance
One class is an extension of another.
Computing with C# and the .NET Framework
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 10 Thinking in Objects
Inheritance, Polymorphism, and Interfaces. Oh My
One class is an extension of another.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Polymorphism and access control
Week 6 Object-Oriented Programming (2): Polymorphism
Encapsulation Inheritance PolyMorhpism
Advanced Java Programming
CS2011 Introduction to Programming I Objects and Classes
Java Programming, Second Edition
Object Oriented Programming
Inheritance.
Workshop for Programming And Systems Management Teachers
Review of Previous Lesson
Chapter 11 Inheritance and Polymorphism Part 1
Programming For Big Data
Chapter 11 Inheritance and Encapsulation and Polymorphism
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

Advanced Object-Oriented Programming David Meredith dave@create.aau.dk

Source Chapter 22 of Shiffman, D. (2008). Learning Processing. Morgan Kaufmann, Burlington, MA.

Introduction Remember objects and classes? An object contains data in variables and has its own functions, called methods, that you call by using the ‘dot’ syntax: myObject.myMethod(); You make instances of objects by constructing them from a class which is like a cookie-cutter that can be used to make lots of cookies of the same shape

Some important OO concepts Encapsulation Hiding the stuff the user doesn’t need to see Inheritance Making specializations of a class that add extra functionality Polymorphism A variable can store objects of a class and any subclass of that class Method overloading Making a new custom version of a method for a subclass

Encapsulation Encapsulation is hiding away the stuff that the user doesn’t need to know about A device or object is encapsulated when the user can see and use its controls, knows what he or she can achieve with the controls but doesn’t need to know exactly how the device or object carries out the task For example, a car is an encapsulated device because lots of people can drive it using its standard controls without knowing exactly how the car works (literally) under the hood

Breaking encapsulation This sketch is not encapsulated because the user can access the object’s variables directly This means that the programmer cannot change the implementation without also changing the interface

Breaking encapsulation What if the bank charges $1.25 for making a withdrawal? What if the bank changes the withdrawal charge periodically? If you want to use the BankAccount class, you have keep track of the bank’s charges and manually code for them each time you use the class

Good encapsulation User cannot access balance variable directly Must do it via “withdraw()” method This allows bank to add a charge for a withdrawal and change this charge without changing the interface User of BankAccount class will continue to just call account.withdraw(amount) Also have a “getter” function that returns the current value in the balance variable

Getters and setters Make instance variables private Access them using getters and setters Decouples interface from implementation Can change implementation without changing interface

Inheritance Building a simulation of a zoo in which each type of animal is represented by an object Set of attributes depends on type of animal If define independent class for each type of animal then break “write once” principle

Inheritance Inheritance is the process by which a class contains all the attributes and operations of its superclasses Define class Animal which contains attributes common to all animals Define Bird to contain everything in Animal plus wingSpan Define Parrot to contain everything in Bird Define Quadruped to contain everything in Animal plus height, foreLegLength and hindLegLength Define Elephant to contain everything in Quadruped plus trunkLength and tuskLength Here only write each distinct attribute once Elephant inherits from Quadruped which inherits from Animal Bird inherits from Animal Quadruped is a subclass (or derived class) of Animal Quadruped is a superclass (or base class) of Elephant Quadruped is a specialization of Animal Animal is a generalization of Quadruped An Elephant is a Quadruped An Elephant is an Animal

Inheritance in Processing We could define Dog and Cat class independently But we’d then have to repeat code Define same variables and methods in each class

extends and super() When calling superclass’ constructor using super(), this must be the first line in the subclass’ constructor

Adding new variables to subclasses You can add new variables as well as new methods to subclasses Here we add a variable, haircolor, and randomly set it in the Dog’s constructor

Method overriding You can also redefine or override methods inherited from superclass Here, the eat() method from Animal is overridden in Dog

Method overriding with super

Shapes Creates two objects, a Circle and a Square On each frame, jiggles the shapes around and displays them Circle class and Square class extend Shape class Inherit everything defined in Shape class Means we don’t have to re-define things that are already defined in the Shape class

Shape class (the superclass) this.x refers to the instance variable called x Used to distinguish from constructor argument which is also called x Shape.display() sets colour of object and drawing mode, but doesn’t actually draw anything

Circle and Square subclasses Circle and Square classes extend Shape Inherit everything (except constructor) from Shape Need to define their own Constructors because Constructor must have same name as class Need to override display method But also must call Shape’s display method in order to set colour etc.

Polymorphism Make an array of Shapes Put Ellipses and Squares of different colours and sizes into the array Iterate through the array, jiggling each Shape and then drawing it shapes[i] is polymorphic because it can refer to many different types of object Can refer to a Shape or any subclass of Shape

Method overloading You can define two or more methods (or constructors) with the same name in the same class! But you must make sure that the pattern of argument types in each method with the name is different!

Overloaded methods The jiggle() method in the Shape class is overloaded several times

Overloaded constructors The Ellipse constructor is overloaded to allow for the user to provide different pieces of information