Title: Object Orientation with C
1Object Orientation with C
2Interfaces
- Separate the specification of behavior from the
implementation of the behavior. - Interfaces can often replace multiple
inheritance. - Interfaces can extend other interfaces in Java
and C - Example IComparable specifies "CompareTo"
behavior.
/ C IComparable interface / namespace System
public interface IComparable int
CompareTo( object other )
method signature only
methods are automatically "public"
3Interface Example in C
/ Person class implements IComparable / using
namespace System public class Person
IComparable private string lastName private
string firstName ... int CompareTo( object
other ) if ( other is Person ) Person p
(Person) other return lastName.CompareTo(
p.lastName ) else throw new
ArgumentException( "CompareTo argument is not
a Person")
Why implement interface? What is the benefit?
4IComparable CompareTo( object ) int
Person
Student
/ System.Array provides utilities for arrays
/ public class Student Person ... public
class Registrar ... Student iup new
Student100 Array.Sort( iup ) Array.BinarySea
rch( iup, new Student("Shinawat")
) Array.Reverse( iup )
5Properties
- Properties are a way to write accessor and
mutators. - Name of a property can be anything.
public class Student private string
m_studentID public string studentID get
return m_studentID set
m_studentID value
This is a property definition
accessor
mutator "value" is implicit parameter
6Using a Property
- A property looks like accessing an attribute of
object.
Student shin new Student("Taksin",
"Shinawat") shin.studentID "11111111" //
calls "set" method Console.WriteLine("ID is
"shin.studentID)
7Properties (2)
- You don't have to define both "get" and "set".
public class Student private int
m_credits private ArrayList courses new
ArrayList() public string credits get
return m_credits public string
course set if ( value.Length ! 6 )
return if ( courses.Contains( value ) )
return courses.Add( value )
No "set" property is read-only
"set" may silently reject the value
8Style
- Microsoft C
- Java
- C (methods use same name as attribute)
int credit student.Credits student.Credits
1
int credit student.getCredits(
) student.setCredits(credit 1)
int credit student.credits( ) student.credits(
credit 1 )
9Inheritance
- Like Java, C allows only single inheritance.
- A class can implement any number of interfaces.
/ Java / class Person private int
id protected String name ... class
Student extends Person implements Comparable
int compareTo( Object other ) ...
/ C / class Person private int
id protected string name ... class
Student Person, IComparable int CompareTo(
object other ) ...
10Overriding Methods
- Java default is run-time (dynamic) binding.
- C default is compile-time (static) binding,
like C.
/ Java / class Person // runtime binding
(default) public int getID( ) ... // can't
override "final" methods public final String
getName( )... class Student extends Person
// OK to override getID public int getID()
... ...
/ C / class Person // can override
"virtual" methods public virtual int getID( )
... // compile-time binding of getName
public string getName( ) ... class
Student Person // use "override" public
override int getID() ... // getName hides
Person.getName public new string getName() ...
11Overriding classes
- public class MyString extends String
- int compareTo(String other )
- return super.compareToIgnoreCase( other )
-
12virtual versus non-virtual Methods
- virtual override method determined at run-time
- non-virtual new method determined at
compile-time
/ Example / Person p new Student("Joe") p.get
ID() // Student.getID p.getName() //
Person.getName
/ C / class Person // can override
"virtual" methods public virtual int getID( )
... // compile-time binding of getName
public string getName( ) ... class
Student Person // use "override" public
override int getID() ... // getName hides
Person.getName public new string getName() ...
13virtual and non-virtual methods
- Which type supports polymorphism?
- virtual override
- default new
- Which type is more efficient?
14C Delegates
- Delegate is a type in the C type system.
- It provides a way to pass a function name as
argument. - A delegate can be a collection of objects.
/ define a delegate that accepts string
/ public delegate void TellMe( string msg )
Any method that has 1 string parameter and
returns "void" can be a TellMe delegate. Examples
out.WriteLine( ), Button.setText( ),
TextArea.append( )
15C Delegates
- Delegate is a type in the C type system.
- It provides a way to pass a function name as
argument. - Can act as a collection for delegates.
/ define a delegate that accepts string
/ public delegate void Observer( string msg )
/ create some delegates / Observer observers
null // empty collection observers new
Observer( out.WriteLine ) observers new
Observer( button.SetText ) observers new
Observer( textarea.Append ) / call all the
observers at once! / observers("Wake Up!")
16Implementing Observer Pattern
"Observer" - use a delegate (instead of interface)
/ define a delegate for Observers / public
delegate void Observer( Observable sender, Object
arg )
"Observable" will invoke this method when it
needs to notify observers. How do we define a
base class for "Observable"?
17Implementing Observer Pattern (2)
"Observable" - use a delegate (instead of
interface)
/ define observable / public class Observable
private Observer observers null public
void addObserver(Observer o) observers
o public void deleteObserver(Observer o)
observers - o public void notifyAll( )
if ( observers ! null ) observers( this,
null )
18Generic Classes
- C supports generic classes.
- Static attributes can use type parameter, too.
class QueueltEgt private E que
private static E first private int
nextIndex public Queue( int capacity )
que new Ecapacity
nextIndex 0 public void
append(E value) if ( first null
) first value quenextIndex
value nextIndex nextIndex
que.Length