Download presentation
Presentation is loading. Please wait.
Published byJamar Brewington Modified over 10 years ago
1
Object Oriented Programming (OOP) with Java
2
Classes and Objects Inheritance Polymorphism
3
Classes and Objects
4
class Dog { String name; String color; }
5
class Dog { String name; String color; } Dog d1 = new Dog();
6
Dog
7
class Dog { String name; String color; } Dog d1 = new Dog(); Dog d2 = new Dog();
8
Dog
9
class Dog { String name; String color; Dog(String n, String c) { name=n; color=c; } Dog d3 = new Dog(“fido”,”brown”); Dog d4 = new Dog (“rex”,”white”);
11
class Dog { String name; String color; Dog(String n, String c) { name=n; color=c; } String getName() { return name;} String getColor() { return color;} }
12
System.out.println(“d3 name=“+d3.getName()); d3 name = fido System.out.println(“d3 color=“+d3.getColor()); d3 color = brown System.out.println(“d4 name=“+d4.getName()); d4 name = rex System.out.println(“d4 color=“+d4.getColor()); d4 color = white
13
class Dog { String name; String color; Dog(String n, String c){ name=n; color=c; } String getName() { return name;} String getColor() { return color;} void setName(String n){name=n;} void setColor(String c){color=c;} }
14
d3.setColor(“red”); d4.setName(“muffie”);
16
System.out.println(“d3 name=“+d3.getName()); d3 name = fido System.out.println(“d3 color=“+d3.getColor()); d3 color = red System.out.println(“d4 name=“+d4.getName()); d4 name = muffie System.out.println(“d4 color=“+d4.getColor()); d4 color = white
17
Inheritance
18
class Animal { name color height weight doeat() doplay() } class Horse { name color height weight doeat() doplay() } class Dog { name color height weight doeat() doplay() } class Cow { name color height weight doeat() doplay() }
19
class Animal { name color height weight doeat() doplay() } class Dog extends Animal {} class Horse extends Animal{} class Cow extends Animal{}
20
class Horse extends Animal} doneigh() dogallop() } class Animal { name color height weight doeat() doplay() } class Dog extends Animal { dobark() dorun() } class Cow extends Animal { domoo() dostep() }
21
class Commission_based_employee { String firstname; String lastname; String address; String telephone; double comm_rate; Commission_based_employee(String f, String l, String a, String t, double c){ firstname=f; lastname=l; address=a; telephone=t; comm_rate=c; }
22
void setFirstname(String s) { firstname=s;} void setLastname(String s) {lastname=s;} void setAddress(String s) { address=s;} void setTelephone(String s) { telephone=s;} void setComm_rate( double d) { comm_rate=d;} String getFirstname() { return firstname;} String getLastname() { return lastname;} String getAddress() { return address;} String getTelephone() { return telephone;} double getComm_rate() { return comm_rate;} } // end of class declaration // class Commission_based_employee
23
class Salary_plus_commission_based_employee { String firstname; String lastname; String address; String telephone; double comm_rate; double salary; Salary_plus_commission_based_employee( String f, String l,String a, String t, double c,double s){ firstname=f; lastname=l; address=a; telephone=t; comm_rate=c; salary=s; }
24
void setFirstname(String s) { firstname=s;} void setLastname(String s) {lastname=s;} void setAddress(String s) { address=s;} void setTelephone(String s) { telephone=s;} void setComm_rate( double d) { comm_rate=d;} void setSalary( double d) { salary=d;} String getFirstname() { return firstname;} String getLastname() { return lastname;} String getAddress() { return address;} String getTelephone() { return telephone;} double getComm_rate() { return comm_rate;} double getSalary() { return salary;} } // end of class declaration //class Salary_plus_commission_based_employee
25
class Salary_plus_commission_based_employee { String firstname; String lastname; String address; String telephone; double comm_rate; double salary; Salary_plus_commission_based_employee( String f, String l,String a, String t, double c,double s){ firstname=f; lastname=l; address=a; telephone=t; comm_rate=c; salary=s; }
26
void setFirstname(String s) { firstname=s;} void setLastname(String s) {lastname=s;} void setAddress(String s) { address=s;} void setTelephone(String s) { telephone=s;} void setComm_rate( double d) { comm_rate=d;} void setSalary( double d) { salary=d;} String getFirstname() { return firstname;} String getLastname() { return lastname;} String getAddress() { return address;} String getTelephone() { return telephone;} double getComm_rate() { return comm_rate;} double getSalary() { return salary;} } // end of class declaration //class Salary_plus_commission_based_employee
27
class Salary_plus_commission_based_employee2 extends Commission_based_employee{ double salary; Salary_plus_commission_based_employee2( String f, String l,String a, String t, double c,double s){ firstname=f; lastname=l; address=a; telephone=t; comm_rate=c; salary=s; void setSalary( double d) { salary=d;} double getSalary() { return salary;} }
28
class Salary_plus_commission_based_employee3 extends Commission_based_employee{ double salary; Salary_plus_commission_based_employee3( String f, String l,String a, String t, double c,double s){ super(f,l,a,t,c); salary=s; void setSalary( double d) { salary=d;} double getSalary() { return salary;} }
29
POLLS
30
Polymorphism
31
class Animal { move() {print(“generic animal movement”);} } class Fish extends Animal { } class Frog extends Animal { } class Bird extends Animal { }
32
Animal a = new Animal(); Fish f = new Fish(); Frog r = new Frog(); Bird b = new Bird();
34
a.move() “animal generic move…” f.move() “animal generic move …” r.move() “animal generic move …” b.move() “animal generic move …”
35
class Animal { move() {print(“generic animal movement”);} } class Fish extends Animal { move() {print(“fish swims 1 ft”);} } class Frog extends Animal { move() {print(“frog jumps 2 ft”);} } class Bird extends Animal { move() {print(“bird flies 3 ft”);} {
36
Animal a = new Animal(); Fish f = new Fish(); Frog r = new Frog(); Bird b = new Bird();
38
a.move() “animal generic move” f.move() “fish swims…” r.move() “frog jumps…” b.move() “bird flies…”
39
Suppose we wish to create capabilities to carry out ten moves in one fell swoop for each of our objects: animal, fish, frog, bird.
40
One way of achieving this objective is to code four routines: animalmove10(){ for (int i=1;i<=10;i++) {a.move();} } fishmove10(){ for (int i=1;i<=10;i++) {f.move();} } frogmove10(){ for (int i=1;i<=10;i++) {r.move();} } birdmove10(){ for (int i=1;i<=10;i++){b.move();} }
41
Alternatively, we can achieve the same objective in a more efficient manner using polymorphism
42
If something is ‘polymorphic’, we mean it has ‘many forms’ What does this mean in the context of java programming?
43
class Animal { move() {print(“generic animal movement”);} } class Fish extends Animal { move() {print(“fish swims 1 ft”);} } class Frog extends Animal { move() {print(“frog jumps 2 ft”);} } class Bird extends Animal { move() {print(“bird flies 3 ft”);} {
44
Animal a = new Animal(); Fish f = new Fish(); Frog r = new Frog(); Bird b = new Bird();
46
Animal aa = new Animal(); Animal af = new Fish(); Animal ar = new Frog(); Animal ab = new Bird();
48
aa.move() “animal generic move” af.move() “fish swims…” ar.move() “frog jumps…” ab.move() “bird flies…”
49
We will now code a new routine which we shall call ‘polymorphicmove10’ and which will accept one argument, a reference variable of type ‘Animal’
50
void polymorphicmove10(Animal x) { for (int i=1,i<=10;i++) { x.move(); }
51
Can we supply reference variable ‘aa’ to our new routine which requires a reference variable of type ‘Animal’? Can we invoke routine ‘polymorphicmove10’ with argument ‘aa’? YES - REFERENCE VARIABLE ‘aa’ IS OF TYPE ‘Animal’ polymorphicmove10( aa );
52
What is the result? The object that reference variable ‘aa’ is referring to has its ‘move()’ method called 10 times. The result is ten prints of : “generic animal move…”
53
Can we supply reference variable ‘af’ to our new routine which requires a reference variable of type ‘Animal’? Can we invoke routine ‘polymorphicmove10’ with argument ‘af’? YES - REFERENCE VARIABLE ‘af’ IS OF TYPE ‘Animal’ polymorphicmove10( af );
54
What is the result? The object that reference variable ‘af’ is referring to has its ‘move()’ method called 10 times. The result is ten prints of : “fish swims…”
55
Can we supply reference variable ‘ar’ to our new routine which requires a reference variable of type ‘Animal’? Can we invoke routine ‘polymorphicmove10’ with argument ‘ar’? YES - REFERENCE VARIABLE ‘ar’ IS OF TYPE ‘Animal’ polymorphicmove10( ar );
56
What is the result? The object that reference variable ‘ar’ is referring to has its ‘move()’ method called 10 times. The result is ten prints of : “frog jumps…”
57
Can we supply reference variable ‘ab’ to our new routine which requires a reference variable of type ‘Animal’? Can we invoke routine ‘polymorphicmove10’ with argument ‘ab’? YES - REFERENCE VARIABLE ‘ab’ IS OF TYPE ‘Animal’ polymorphicmove10( ab );
58
What is the result? The object that reference variable ‘ab’ is referring to has its ‘move()’ method called 10 times. The result is ten prints of : “bird flies…”
59
Therefore, instead of coding, testing, and debugging four routines: animalmove10(){ for (int i=1;i<=10;i++) {a.move();} } fishmove10(){ for (int i=1;i<=10;i++) {f.move();} } frogmove10(){ for (int i=1;i<=10;i++) {r.move();} } birdmove10(){ for (int i=1;i<=10;i++){b.move();} }
60
we code, test, debug, and use: void polymorphicmove10(Animal x) { for (int i=1,i<=10;i++) { x.move(); }
61
class Animal { … } class Duck extends Animal { void walk ( print “duck walking”); } class Person { void walk ( print “person walking”); }
62
Duck d = new Duck(); Person p = new Person();
64
Objective: walk 10 times. void duckwalk10() { for (int i=1;i<=10;i++) { d.walk();} } void personwalk10() { for (int i=1;i<=10;i++) {p.walk();} }
65
We want a polymorphicwalk10 routine which will work for both ducks and persons. void polymorphicwalk10(aGenericWalker w) { for (int i=1;i<=10;i++) { w.walk();} }
66
class Walker { void walk() { print “generic walking” } } class Person extends Walker { void walk() { print “person walking” } } class Duck extends Animal, Walker { void walk() { print “duck walking”} }
67
class Walker { void walk() { print “generic walking” } } class Person extends Walker { void walk() { print “person walking” } } class Duck extends Animal, Walker { void walk() { print “duck walking”} }
68
It is illegal in Java to extend more than one class. It may not be possible to change the inheritance of an existing class. If we want to add the ability to ‘walk’ to objects of an existing class, we should not be forced to change the inheritance of the class. In our case, we may not be able to change the inheritance of the ‘Duck’ class from ‘Animal’ to ‘Walker’.
69
Solution: Interfaces a class can extend from one class and ‘implement’ one or more interfaces at the same time
70
interface Walker { void walk(); } class Person implements Walker { void walk() { print “person walking” } } class Duck extends Animal implementsWalker { void walk() { print “duck walking”} }
71
Walker wd = new Duck(); Walker wp = new Person();
73
We want a polymorphicwalk10 routine which will work for both ducks and persons. void polymorphicwalk10(Walker w) { for (int i=1;i<=10;i++) { w.walk();} }
74
Can we supply reference variable ‘wd’ to our new routine which requires a reference variable of type ‘Walker’? Can we invoke routine ‘polymorphicwalk10’ with argument ‘wd’? YES - REFERENCE VARIABLE ‘wd’ IS OF TYPE ‘Walker’ polymorphicwalk10( wd );
75
What is the result? The object that reference variable ‘wd’ is referring to has its ‘walk()’ method called 10 times. The result is ten prints of : “duck walking”
76
Can we supply reference variable ‘wp’ to our new routine which requires a reference variable of type ‘Walker’? Can we invoke routine ‘polymorphicwalk10’ with argument ‘wp’? YES - REFERENCE VARIABLE ‘wp’ IS OF TYPE ‘Walker’ polymorphicwalk10( wp );
77
What is the result? The object that reference variable ‘wp’ is referring to has its ‘walk()’ method called 10 times. The result is ten prints of : “person walking”
78
class GameShape { public void displayShape() { System.out.println("displaying shape");} } class PlayerPiece extends GameShape { public void movePiece() { System.out.println("moving game piece");} } class TilePiece extends GameShape { public void getAdjacent() { System.out.println("getting adjacent tiles");} }
79
public class TestShapes { public static void main (String[] args) { PlayerPiece player = new PlayerPiece(); TilePiece tile = new TilePiece(); doShapes(player); doShapes(tile); } public static void doShapes (GameShape shape) { shape.displayShape();} }
80
public class Animal { public void eat() { System.out.println( "Generic Animal Eating Generically");} } public class Horse extends Animal { public void eat() { System.out.println("Horse eating hay");} public void eat(String s) { System.out.println("Horse eating " + s);} }
81
Animal a = new Animal(); a.eat(); //Generic Animal Eating Generically Horse h = new Horse(); h.eat(); //Horse eating hay Animal ah = new Horse(); ah.eat(); //Horse eating hay - Polymorphism works. Horse he = new Horse(); he.eat("Apples"); //Horse eating Apples -overloaded method used Animal a2 = new Animal(); a2.eat("treats"); //Compiler error. Animal ah2 = new Horse(); ah2.eat("Carrots"); //Compiler error
82
Object Oriented Programming (OOP) with Java
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.