0% found this document useful (0 votes)
71 views

CS 1101-03 Programming and Problem Solving: Fall 2016

tfgyhjkl

Uploaded by

Nick Heilborn
Copyright
© © All Rights Reserved
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)
71 views

CS 1101-03 Programming and Problem Solving: Fall 2016

tfgyhjkl

Uploaded by

Nick Heilborn
Copyright
© © All Rights Reserved
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/ 21

CS 1101-03

Programming and Problem Solving

Fall 2016
Terminology
A class from which other classes are derived. The
Base class is ?? derived classes inherit properties of the base class.

Another name for base class….


Super class ??
If class A is a super class of class B which is a super
Ancestor class is ?? class of class C, then class A is an ancestor of class C

Descendant class ?? If class A is a super class of class B which is a super


class of class C, then class C is a descendant of class A

Overloading is ?? Same method name – different parameters (number


or type).

Same method name, same parameters (number or


Overriding is ?? type) but does something different.

Copyright © 2010
An Object Can Have More than One Type
class Person{ We can have:
private String name;
……
void printMe (){
System.out.println (“Person.”)
}

class Student extends Person{ We can also have:


private int stdNb;
……
void printMe (){
System.out.println (“Student.”)
}
Every Student is a Person
class Undergraduate extends Student{
private int level;
……
Every Undergraduate is a Student
void printMe (){ and every Undergraduate is a Person also
System.out.println (“UndGrad.”)
}

Poly = "many"
This is known as Polymorphism Morph = "shape"

Copyright © 2010
And so what ?
Example of one situation where polymorphism is useful:
We want to keep everybody in the same
array structure for easy access.
But we should have only one type in an
array… so this is where polymorphism
comes to play:

Person [ ] university = new Person [10000];


university [0] = new Student();
university [1] = new Student(); We can group many
…. objects together even if
university[1000] = new Doctoral(); they are different type.
….
university [8000] = new Employee();
…etc.

Copyright © 2010
An Object Can Have More than One Type
class Person{ What is the output ?
private String name;
……
void printMe (){
System.out.println (“Person.”)
}
class Student extends Person{
private int stdNb;
……
void printMe (){
System.out.println (“Student.”)
}
class Undergraduate extends Student{ Java will chooses the true type: the object
private int level; that actually gets created. In this example,
…… what we are really creating are a Person, a
void printMe (){ Student and an UnderGrad, even though we
System.out.println (“UndGrad.”)
}
are referencing all variables as a Person.

Dynamic Binding:
Different objects can invoke different method definitions using the same method name.
Warning:
Student s = new Student();
Person p = s; //p has only access to methods of the class Person
Copyright © 2010
Which method are actually invoked ?
Class Person can call: Class Undergraduate can call:
• Its own constructor • Its own constructor
• setName(String) • setName(string)
• getName() • getName()
• toString() • getStudentNumber()
• equals(Object) • setStudentNumber(int)
• reset(String, int)
• getlevel()
Class Student can call: • setLevel(int)
• Its own constructor • reset(String, int, int)
• setName(String) • toString()
• getName() • equals(Object)
• getStudentNumber()
• setStudentNumber(int)
• reset(String, int)
• toString()
• equals(Object)

Copyright © 2010
Which method are actually invoked ?
Class Person can call: Class Undergraduate can call:
• Its own constructor • Its own constructor
• setName(String) • setName(string)
• getName() • getName()
• toString() • getStudentNumber()
• equals(Object) • setStudentNumber(int)
• reset(String, int)
• getlevel()
Class Student can call: • setLevel(int)
• Its own constructor • reset(String, int, int)
• setName(String) • toString()
• getName() • equals(Object)
• getStudentNumber()
• setStudentNumber(int) Inherited from ancestor class
• reset(String, int)
• toString()
Person: these methods are only
• equals(Object) written once!

Copyright © 2010
Which method are actually invoked ?
Class Person can call: Class Undergraduate can call:
• Its own constructor • Its own constructor
• setName(String) • setName(string)
• getName() • getName()
• toString() • getStudentNumber()
• equals(Object) • setStudentNumber(int)
• reset(String, int)
• getlevel()
Class Student can call: • setLevel(int)
• Its own constructor • reset(String, int, int)
• setName(String) • toString()
• getName() • equals(Object)
• getStudentNumber()
• setStudentNumber(int) Inherited from super class
• reset(String, int)
• toString()
Student: these methods are
• equals(Object) only written once!
Note: they are not available for
Copyright © 2010
a Person type.
Which method are actually invoked ?
Class Person can call: Class Undergraduate can call:
• Its own constructor • Its own constructor
• setName(String) • setName(string)
• getName() • getName()
• toString() • getStudentNumber()
• equals(Object) • setStudentNumber(int)
• reset(String, int)
• getlevel()
Class Student can call: • setLevel(int)
• Its own constructor • reset(String, int, int)
• setName(String) • toString()
• getName() • equals(Object)
• getStudentNumber()
• setStudentNumber(int) Only Undergraduate types can call
• reset(String, int)
• toString()
reset(String, int, int) getLevel()
• equals(Object) and setLevel(int). They’re the
only one’s with a copy.
The overloaded version of reset
Copyright © 2010
Which method are actually invoked ?
Class Person can call: Class Undergraduate can call:
• Its own constructor • Its own constructor
• setName(String) • setName(string)
• getName() • getName()
• toString() • getStudentNumber()
• equals(Object) • setStudentNumber(int)
• reset(String, int)
• getlevel()
Class Student can call: • setLevel(int)
• Its own constructor • reset(String, int, int)
• setName(String) • toString()
• getName() • equals(Object)
• getStudentNumber()
• setStudentNumber(int) toString() and equals are
• reset(String, int)
• toString()
overriden.
• equals(Object) Version called depends on the
object making the call.
Copyright © 2010
And so what (again)?
Elaborate on this situation:
Person [ ] university = new Person [10000];
university [0] = new Student();
university [1] = new Student();
We have a mix of
….
university[1000] = new Doctoral();
types in the
…. same array.
university [8000] = new Employee();
…etc.

Suppose that we have a method boolean pass () implemented in all classes:


• Person, Student, Employee, Faculty, Staff the method returns true.
• Undergraduate: returns true if grade > 60
• Graduate: returns true if grade > 70
• Masters, Doctoral: returns true if grade > 80

for (Person p: university){


p.pass(); The code to determine pass or not is
} calculated in the appropriate class.

Copyright © 2010
A Polymorphism Problem
Assume that the following four classes have been declared:
public class Foo {
public void method1() {
System.out.println("foo 1"); }

public void method2() {


System.out.println("foo 2"); }

public String toString() {


return "foo"; }
}

public class Bar extends Foo { public class Baz extends Foo {
public void method2() { public void method1() {
System.out.println("bar 2"); } System.out.println("baz 1"); }
}
public String toString() {
return "baz"; }
}

public class Mumble extends Baz {


public void method2() {
System.out.println("mumble 2"); }
}
Copyright © 2010
A Polymorphism Problem
• What would be the output of the following client code?
Foo[] pityTheFool = {new Foo(), new Bar(), new Baz(), new Mumble()};

for (int i = 0; i < pityTheFool.length; i++) {


System.out.println(pityTheFool[i]);
pityTheFool[i].method1();
pityTheFool[i].method2();
System.out.println();
}

Output:
foo
foo 1
foo 2

foo
foo 1
bar 2

baz
baz 1
foo 2

baz
baz 1
mumble 2

Copyright © 2010
Diagramming the Classes
• Add classes from top (superclass) to bottom (subclass)
• Include all inherited methods

Copyright © 2010
Finding Output with Tables

method Foo Bar Baz Mumble

method1 foo 1 foo 1 baz 1 baz 1

method2 foo 2 bar 2 foo 2 mumble 2

toString foo foo baz baz

Copyright © 2010
Inheritance
• With class inheritance, we saw we could create
polymorphic objects
o An object could act as many different types
o An Undergraduate object could also act as a Student
object or a Person object
o The type of the actual object determined which
version of overridden methods were executed
• Sometimes we want to have this ability of
different classes overriding methods, but do not
want/need the capabilities of full inheritance
• Class interfaces fill this need
Interface – example.
Consider this hierarchy of 2D objects.
We know each object will have
Shape - An inside color
- A position
- A type of outline
Class - Some dimensions…etc.
Class Circle - A perimeter
Polygon
- An area…etc.
• The formula to calculate perimeter and area
Class Class will vary with the type of figure.
Triangle Rectangle • We can override these methods…
• In the class Shape these methods will be
empty…
/**
An interface for methods that return
the perimeter and area of an object
*/
public interface Shape { Uses the interface keyword
/** Returns the perimeter */
public double getPerimeter(); Methods are not implemented
/** Returns the area */
public double getArea();
A way to say: “all my descendants
} will have these methods.
Copyright © 2010
Java Interfaces
• By convention, interface names begin with uppercase letter (just
like classes)
• Stored in a file with suffix .java (just like classes)
• Interfaces do not include:
o Declarations of constructors
o Instance variables
o Method bodies
• To implement an interface, a class must:
o Include the phrase: implements InterfaceName
o Provide a definition each specified method
o The class can include instance variables and additional methods
Interface Example
public interface Shape {
public double getPerimeter();
public double getArea();
}

/** /**
A class of rectangles. A class of circles.
*/ */
public class Rectangle implements Shape { public class Circle implements Shape {
private double myWidth; private double myRadius;
private double myHeight;
public Circle(double radius) {
public Rectangle(double width, double height) { myRadius = radius;
myWidth = width; }
myHeight = height;
} public double getPerimeter() {
return getCircumference();
public double getPerimeter() { }
return 2 * (myWidth + myHeight);
} public double getCircumference() {
return 2 * Math.PI * myRadius;
public double getArea() { }
return myWidth * myHeight;
} public double getArea() {
return Math.PI * myRadius * myRadius;
// not shown: setWidth, getWidth, etc. }
} // not shown: setRadius, getRadius, etc.
}
An Interface as a Type
• Possible to write a method that has an interface
type as a parameter
• An interface is a reference type, just like a class
• You can then call the method passing it an object of
any class which implements that interface
• Interfaces provide us with another form of
polymorphism but without the need for an inheritance
hierarchy
• Objects share some common behavior without
sharing any common data
An Interface as a Type
public static void display(Shape figure) {
double perim = figure.getPerimeter();
double area = figure.getArea();
System.out.println("Perimeter is " + perim +
"; area is " + area);
}

public static void main(String[] args) {


Rectangle rec1 = new Rectangle(2.0,2.0);
Circle cir1 = new Circle(2.0);
display(rec1);
display(cir1);
} An interface as a parameter

Every class that implements Shape will


know how to get its perimeter and area

You might also like