1 Object Oriented Programming (OOP). Exam Friday Paper questions –6 Subroutines BLAT/BLAST Hash Modules Sorting 2.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
1 OBJECT-ORIENTED CONCEPTS. 2 What is an object?  An object is a software entity that mirrors the real world in some way.  A software object in OOP.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
Object-Oriented PHP (1)
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan
Object-Oriented Programming. Libraries If you have subroutines that you use repeatedly, you will probably be happier if they are kept in a single location.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
CO320 Introduction to Object- Oriented Programming Michael Kölling 3.0.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Introduction to Object-oriented programming and software development Lecture 1.
BCS 2143 Introduction to Object Oriented and Software Development.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Programming in Java CSCI-2220 Object Oriented Programming.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Object Oriented Programing (OOP)
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Object Oriented Programming
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
OOP Basics Classes & Methods (c) IDMS/SQL News
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (1/2)
CS 360 Perl Part 2. Remember  Assignments are Due Today  Make a web page for all of your assignments  You get to design it so it works well for you.
Object Oriented Programming Some Interesting Genes.
Chapter 7 Object-Oriented Programming
Objects First with Java A Practical Introduction using BlueJ
Object-Oriented Programming
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Perl Modules.
Corresponds with Chapter 7
Interface.
Lecture 22 Inheritance Richard Gesick.
Object Oriented Programming in java
Object-Oriented PHP (1)
C++ Object Oriented 1.
Presentation transcript:

1 Object Oriented Programming (OOP)

Exam Friday Paper questions –6 Subroutines BLAT/BLAST Hash Modules Sorting 2

3 Object Oriented Programming (OOP) OOP Pros –programs will take less time to read, write, debug, and maintain –helps for managing large programming projects (especially with multiple coders) OOP Cons –"generally" a program in an OO language will run slower than one written in a language without objects (based on the assumption that abstraction away from the machine code is slower). –"C++ programs have worse instruction cache locality (than C)" Quantifying Behavior Differences Between C and C++ Programs. Calder, Grunwald, Zorn. Journal of Programming Languages, Vol2, Num 4, 1994.

4 Key Concepts and Terminology OOP encapsulates data (attributes) and subroutines (behavior) into packages called "objects" The software instantiation of an object is typically called a "class" data and subroutines are intimately coupled Objects have the property of "information hiding" –objects do not generally know how each other are implemented –communicate via well-defined "interface" –analogy: drive car without knowledge of how transmission works Programming is "object oriented" as opposed to "function oriented" –we have been "function oriented" programming –will continue to be for the rest of the class, however, a basic understanding of OOP allows us to access modules available in BioPerl

5 Key Concepts and Terminology Fundamental principle of good software engineering –separate interface from implementation Makes it easier to modify programs –changing a class's implementation does not affect the "client" as long as the class's interface is unchanged Java to DB direct access VS Java/Perl/PHP VS Webservices example

6 Example in Perl (Class/package) sub Cow::speak { ## here I'm separating "Cow" (the package) from the print "a Cow goes moo\n"; ## "speak" (the subroutine) with "::" } sub Horse::speak { print "a Horse goes neigh\n"; } sub Sheep::speak { print "a Sheep goes baaah\n"; } Cow::speak; Horse::speak; Sheep::speak; #a Cow goes moo #a Horse goes neigh #a Sheep goes baaah ## Nothing new here

7 Method Invocation Arrow "class" is a group of things with similar behaviors and traits Class->method invokes subroutine method in package Class

8 Method Invocation Arrow sub Cow::speak { print "a Cow goes moo\n"; } sub Horse::speak { print "a Horse goes neigh\n"; } sub Sheep::speak { print "a Sheep goes baaah\n"; } Cow->speak; Horse->speak; Sheep->speak; #a Cow goes moo #a Horse goes neigh #a Sheep goes baaah Functionally appears to be NO different, but it is

9 Parts are Separable NOW Allows us to do this: my $beast = "Cow"; $beast->speak; Package name is separated from the subroutine name Can use a variable package name (sort of like interpolation???) Cannot do this: $beast::speak;

10 Separating the Package from the Method sub Cow::speak { print "a Cow goes moo\n"; } sub Horse::speak { print "a Horse goes neigh\n"; } sub Sheep::speak { print "a Sheep goes baaah\n"; } = qw(Cow Cow Horse Sheep Sheep); foreach my $beast { $beast->speak; }

11 Observations Excessive common code –each speak subroutine has similar structure: a print and a string that is mostly similar OOP feature –minimize common code –write in once –test and debug only once

12 Method Invocation with Extra Parameter The invocation of: really does this: meaning you get the class name as the first parameter or the only parameter if no arguments are passed

13 Rewrite Animal Methods sub Cow::speak { my $class = shift; print "a Cow goes moo\n"; } sub Horse::speak { my $class = shift; print "a Horse goes neigh\n"; } sub Sheep::speak { my $class = shift; print "a Sheep goes baaah\n"; } Why bother????

14 Second Method to Simplify { package Cow; sub sound { "moo"} sub speak { my $class = shift; print "a $class goes", $class->sound,"\n"; } # can do same for Horse, and Sheep But only the package name, and specific sound change share the definition for "speak" between the Cow, Horse, and Sheep – with "inheritance"

15 Inheritance Example Rather than duplicate the "speak" subroutine for each animal, make a common subroutine package called Animal: { package Animal; sub speak { my $class = shift; print "a $class goes ", $class->sound, "\n"; } For each animal, you can say it "inherits" from Animal

16 Putting it All Together { package Animal; sub speak { my $class = shift; print "a $class goes ", $class->sound, "\n"; } { package = qw(Animal); sub sound {"mooo"} } Cow->speak; 1)Perl constructs argument list (just "Cow") 2)Perl looks for Cow::speak (not there) 3)Perl checks for the inheritance It exists and contains "Animal". This is a way for "sharing" methods. Note that we've factored out the speak subroutine from all the animals. 4)Perl checks for "speak" inside "Animal" (Animal::speak) and finds it 5)Perl invokes subroutine as if: Animal::speak("Cow"); Note – the excessive curly braces are to emphasize that there are no requirement for symbols to be in the same file or scope. See example next slide. In many ways -- the explicit passing "information" between code, subroutines and packages is rather appealing for understanding OOP and inheritance.

17 #!/usr/bin/perl package Animal; sub speak { my $class = shift; print "a $class goes ",$class->sound,"\n"; } package = qw(Animal); sub sound { "moo"} package = qw(Animal); sub sound { "neigh"} package = qw(Animal); sub sound { "baah"} Cow->speak; Horse->speak; Sheep->speak; # a Cow goes moo # a Horse goes neigh # a Sheep goes baah

18 A slightly more sophisticated example

19 bless bless REF,CLASSNAME bless REF This function tells the thingy referenced by REF that "it" is now an object in the CLASSNAME package. If CLASSNAME is omitted, the current package is used. Because a "bless" is often the last thing in a constructor, it returns the reference for con- venience. Always use the two-argument version if the function doing the blessing might be inherited by a derived class. See perltoot and perlobj for more about the blessing (and bless- ings) of objects.

20 package Gene1; use strict; use warnings; use Carp; #gives us "croak" sub new { my ($class, %arg) # Note -- %arg is a hash! return bless # bless REFERENCE, CLASS { # general convention UNDERSCORE means internal variable # Next line -- we access the hash (passed in) -- to assign the value to "this" hash _name => $arg{name} || croak ("no name"), _organism => $arg{organism} || croak ("no organism"), _chromosome => $arg{chromosome} || ("no chromosome"), _pdbref => $arg{pdbref} || ("no pdbref"), },$class; #returned hash reference 'marked' with "Gene1" } sub name { $_[0] -> {_name} } # the method receives the object (Gene1) as the first parameter # which in this case is just a reference to a hash. The data is abstracted away in this fashion, so that if we later change how the data is stored users of this 'interface' can access the data in the same way. #simple keys -- don't need quotes sub organism { $_[0] -> {_organism} } sub chromosome { $_[0] -> {_chromosome} } sub pdbref { $_[0] -> {_pdbref} } 1;

21 Frankly -- the previous example is one reason people sometimes do not like perl. The way it is written 'obfuscates' what is going on.

22 package Gene1; #Gene1.pm use strict; use warnings; use Carp; #gives us "croak" sub new { my ($class, %arg) # $_hash is an internal hash my %_hash =(); $_hash{_name} = $arg{name} || croak("No name"); $_hash{_organism} = $arg{organism} || croak("No organism"); $_hash{_chromosome} = $arg{chromosome} || "No chromosome"; $_hash{_pdbref} = $arg{pdbref} || "No PDB reference"; bless \%_hash,$class; #hash is 'marked' with Gene1 return(\%_hash); # return a reference to a hash } sub name { $_[0] -> {_name} } # the method receives the object as the first parameter # which in this case is just a reference to a hash # The data is abstracted away in this fashion, # so that if we later change how the data is stored # users of this 'interface' can access the # data in the same way. sub organism { $_[0] -> {_organism} } sub chromosome { $_[0] -> {_chromosome} } sub pdbref { $_[0] -> {_pdbref} } print "hellow world\n"; 1;

23 use strict; #testGene1.pl use warnings; use Gene1; print "Ojbect 1:\n\n"; my $obj1 = Gene1->new ( ## passing a hash into a subroutine!!! name => "BBS4", organism => "Homo sapiens", chromosome => "15", pdbref => "pdb999.ent" ); print $obj1->name, "\n"; print $obj1->organism, "\n"; print $obj1->chromosome, "\n"; print $obj1->pdbref, "\n";

24 print "\nOjbect 2:\n\n"; my $obj2 = Gene1->new ( name => "Abca4", organism => "Mus musculus"); print $obj2->name, "\n"; print $obj2->organism, "\n"; print $obj2->chromosome, "\n"; print $obj2->pdbref, "\n"; print "\n"; print '"bless" tags the reference with the object/package name'."\n"; print 'print ref $obj1, "\n";'."\n"; print ref $obj1, "\n";

25 print "\nOjbect 3:\n\n"; my $obj3 = Gene1->new ( organism => "Homo Sapiens", chromosome => "15", pdbref => "pdb999.ent" ); print $obj3->name, "\n"; print $obj3->organism, "\n"; print $obj3->chromosome, "\n"; print $obj3->pdbref, "\n";