0% found this document useful (0 votes)
53 views23 pages

Object Oriented Programming (OOP)

Object Oriented Programming (OOP) encapsulates data and subroutines into objects. OOP has pros like easier maintenance of large programs with multiple coders, but can have cons like slower program execution speed compared to non-OOP languages. Key OOP concepts include classes which instantiate objects, information hiding where objects communicate via interfaces without knowing internal implementations, and inheritance where objects can inherit behaviors from parent objects. An example in Perl shows defining an Animal class and inheriting behaviors for Cow, Horse, and Sheep classes to share a common speak method.

Uploaded by

muttuswami
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views23 pages

Object Oriented Programming (OOP)

Object Oriented Programming (OOP) encapsulates data and subroutines into objects. OOP has pros like easier maintenance of large programs with multiple coders, but can have cons like slower program execution speed compared to non-OOP languages. Key OOP concepts include classes which instantiate objects, information hiding where objects communicate via interfaces without knowing internal implementations, and inheritance where objects can inherit behaviors from parent objects. An example in Perl shows defining an Animal class and inheriting behaviors for Cow, Horse, and Sheep classes to share a common speak method.

Uploaded by

muttuswami
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Object Oriented Programming

(OOP)

1
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 Programming2
Languages, Vol2, Num 4, 1994.
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
3
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

4
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

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

6
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

7
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;
8
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";
}

my @farm = qw(Cow Cow Horse Sheep Sheep);


foreach my $beast (@farm) {
$beast->speak;
}

9
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

10
Method Invocation
with Extra Parameter
The invocation of:
Class->method(@args)
really does this:
Class->method("Class",@args)
meaning you get the class name as the first
parameter or the only parameter if no
arguments are passed

11
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????

12
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"

13
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

14
Putting it All Together
{ package Animal;
sub speak {
my $class = shift;
print "a $class goes ", $class->sound, "\n";
}
}

{ package Cow;
@ISA = 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 array @Cow::ISA 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.

15
#!/usr/bin/perl

package Animal;

sub speak {
my $class = shift;
print "a $class goes ",$class->sound,"\n";
}

package Cow;
@ISA = qw(Animal);
sub sound { "moo"}

package Horse;
@ISA = qw(Animal);
sub sound { "neigh"}

package Sheep;
@ISA = qw(Animal);
sub sound { "baah"}

Cow->speak;
Horse->speak;
Sheep->speak;

# a Cow goes moo


# a Horse goes neigh
# a Sheep goes baah

16
A slightly more sophisticated
example

17
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} }
18
1;
Frankly -- the previous example is one
reason people sometimes do not like perl.

The way it is written 'obfuscates' what is


going on.

19
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";


20
1;
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";

21
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";

22
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";

23

You might also like