The document discusses key concepts in object-oriented programming including classes, methods, interfaces, properties, and nested classes. It provides examples of class definitions in various languages like Java, C++, and C# to illustrate concepts like encapsulation, visibility modifiers, constructors, and accessor/mutator methods. It also covers topics like separation of definition and implementation, interfaces, properties, and class data fields.
Corporate Management | Session 3 of 3 | Tendenci AMS
Classes And Methods
1. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
CLASSES AND METHODS
Muhammad Adil Raja
Roaming Researchers, Inc.
cbnd
April 14, 2015
2. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
OUTLINE I
1 INTRODUCTION
2 ENCAPSULATION AND INSTANTIATION
3 CLASS DEFINITIONS
4 METHODS
5 INTERFACES IN JAVA
6 PROPERTIES
7 INNER OR NESTED CLASSES
8 CLASS DATA FIELDS
9 SUMMARY
10 REFERENCES
3. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
INTRODUCTION I
Chapters 4 and 5 discuss two sides of OOP.
Chapter 4 discusses the static, compile time
representation of object-oriented programs.
Chapter 5 discusses the dynamic, run time behavior
Both are important, and both chapters should be understood
before you begin further investigation of object-oriented
programming.
4. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
SAME IDEAS DIFFERENT TERMS I
All OOP languages have the following concepts, although the
terms they use may differ:
CLASSES: object type, factory object.
INSTANCES: objects
MESSAGE PASSING: method lookup, member function
invocation, method binding.
METHODS: member function, method function
INHERITANCE: subclassing
5. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
ENCAPSULATION AND INSTANTIATION I
ENCAPSULATION
The purposeful hiding of information, thereby reducing the
amount of details that need to be remembered/communicated
among programmers.
A SERVICE VIEW
The ability to characterise an object by the service it provides,
without knowing how it performs its task.
INSTANTIATION
The ability to create multiple instances of an abstraction.
6. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
INTERNAL AND EXTERNAL VIEWS I
TWO VIEWS OF SOFTWARE
Encapsulation means there are two views of the same system.
The outside, or service view, describes what an object does.
The inside, or implementation view, describes how it does it.
7. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
BEHAVIOR AND STATE I
A class can also be viewed as a combination of behavior and
state.
BEHAVIOR: The actions that an instance can perform in
response to a request. Implemented by methods.
STATE: The data that an object must maintain in order to
successfully complete its behavior. Stored in
instance variables (also known as data members,
or data fields).
8. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
CLASS DEFINITIONS I
We will use as a running example the class definition for a
playing card abstraction, and show how this appears in
several languages.
Languages include Java, C++, C#, Delphi Pascal, Apple
Pascal, Ruby, Python, Eiffel, Objective-C and Smalltalk.
9. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
CLASS DEFINITIONS II
A TYPICAL EXAMPLE, CLASS DEFINITION IN C++
class PlayingCard {
public :
enum Suits {Spade , Diamond , Club , Heart } ;
Suits s u i t ( ) { return suitValue ; }
int rank ( ) { return rankValue ; }
private :
Suits suitValue ;
int rankValue ;
} ;
10. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
CLASS DEFINITIONS III
Note syntax for methods, data fields, and visibility modifiers.
(Will see more on syntax later).
11. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
VISIBILITY MODIFIERS I
The terms public and private are used to differentiate the
internal and external aspects of a class.
Public features can be seen and manipulated by anybody –
they are the external (interface or service) view.
Private features can be manipuated only within a class.
They are the internal (implementation) view.
Typically methods are public and data fields are private, but
either can be placed in either category.
12. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
VISIBILITY MODIFIERS II
A C-SHARP CLASS DEFINITION
enum Suits {Spade , Diamond , Club , Heart } ;
class PlayingCard {
public Suits s u i t ( ) { return suitValue ; }
public int rank ( ) { return rankValue ; }
private Suits suitValue ;
private int rankValue ;
}
C# class definitions have minor differences, no semicolon at
end, enum cannot be nested inside class, and visibility
modifiers are applied to methods and data fields individually.
13. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
VISIBILITY MODIFIERS III
JAVA CLASS DEFINITION
class PlayingCard {
public int s u i t ( ) { return suitValue ; }
public int rank ( ) { return rankValue ; }
private int suitValue ;
private int rankValue ;
public static f i n a l int Spade = 1;
public static f i n a l int Diamond = 2;
public static f i n a l int Club = 3;
public static f i n a l int Heart = 4;
}
14. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
VISIBILITY MODIFIERS IV
Java also applies visibility modifiers to each item indivually.
Does not have enumerated data types, uses symbolic
constants instead.
15. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
STATIC AND FINAL I
Notice how symbolic constants are defined in Java:
static means that all instance share the same value. One
per class. Similar meaning in many languages.
final is Java specific, and means it will not be reassigned.
(C++ has const keyword that is similar, although not
exactly the same).
STATIC AND FINAL
public static f i n a l int Spade = 1;
public static f i n a l int Diamond = 2;
public static f i n a l int Club = 3;
public static f i n a l int Heart = 4;
16. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
METHODS I
Although syntax will differ depending upon language, all
methods have the following:
A name that will be matched to a message to determine
when the method should be executed.
A signature, which is the combination of return type and
argument types. Methods with the same name can be
distinguished by different signatures.
A body, which is the code that will be executed when the
method is invoked in response to a message.
17. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
METHODS II
AN EXAMPLE FROM C-SHARP
class PlayingCard {
/ / constructor , i n i t i a l i z e new playing card
public PlayingCard ( Suits is , int i r )
{ s u i t = i s ; rank = i r ; faceUp = true ; }
/ / operations on a playing card
public boolean isFaceUp ( ) { return faceUp ; }
public int rank ( ) { return rankValue ; }
public Suits s u i t ( ) { return suitValue ; }
public void setFaceUp ( boolean up ) { faceUp = up ; }
public void f l i p ( ) { setFaceUp ( ! faceUp ) ; }
public Color color ( ) {
i f ( ( s u i t ( ) == Suits . Diamond ) | | ( s u i t ( ) == Suits . Heart ) )
return Color . Red ;
return Color . Black ;
}
/ / private data values
private Suits suitValue ;
private int rankValue ;
private boolean faceUp ;
}
18. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
METHODS III
CONSTRUCTOR
class PlayingCard {
/ / constructor , i n i t i a l i z e new playing card
public PlayingCard ( Suits is , int i r )
{ s u i t = i s ; rank = i r ; faceUp = true ; }
. . .
}
A constructor is a method that is used to initialize a newly
constructed object.
In C++, Java, C# and many other languages.
It has the same name as the class.
19. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
METHODS IV
ACCESSOR (OR GETTER) METHODS
class PlayingCard {
. . .
/ / operations on a playing card
public int rank ( ) { return rankValue ; }
public Suits s u i t ( ) { return suitValue ; }
. . .
private int rankValue ;
}
An accessor (or getter) is a method that simply returns an
internal data value:
Why Use an Accessor? There are many reasons why an
accessor is preferable to providing direct access to a data
field.
You can make the data field read-only.
20. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
METHODS V
It provides better documentation that the data field is
accessible
It makes it easier to later change the access behavior
(count number of accesses, whatever).
Some conventions encourage the use of a name that
begins with get, (as in getRank()), but this is not universally
followed.
SETTERS (OR MUTATORS)
class PlayingCard {
/ / operations on a playing card
public void setFaceUp ( boolean up ) { faceUp = up ; }
. . .
/ / private data values
private boolean faceUp ;
}
21. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
METHODS VI
A setter (sometimes called a mutator method) is a method
that is used to change the state of an object.
Mutators are less common than accessors, but reasons for
using are similar.
Constant data fields.
Some languages allow data fields to be declared as
constant (const modifier in C++, final in Java, other
languages have other conventions).
Constant data fields can be declared as public, since they
cannot be changed.
22. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
ORDER OF METHODS I
For the most part, languages don’t care about the order
that methods are declared.
Here are some guidelines:
List important topics first.
Constructors are generally very important, list them first.
Put public features before private ones.
Break long lists into groups
List items in alphabetical order to make it easier to search.
Remember that class definitions will often be read by
people other than the original programmer.
Remember the reader, and make it easy for them.
23. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
SEPARATION OF DEFINITION AND IMPLEMENTATION I
In some languages (such as C++ or Object Pascal) the
definition of a method can be separated from its
implementation.
They may even be in a different file:
SEPARATION OF DEFINITION AND IMPLEMENTATION
class PlayingCard {
public :
. . .
Colors color ( ) ;
. . .
} ;
PlayingCard : : Colors PlayingCard : : color ( )
{
/ / return the face color of a playing card
i f ( ( s u i t == Diamond ) | | ( s u i t == Heart ) )
return Red ;
return Black ;
}
24. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
SEPARATION OF DEFINITION AND IMPLEMENTATION II
Notice the need for fully-qualified names.
25. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
CONSIDERATIONS IN METHOD DEFINITIONS I
In C++ you have a choice to define a method in the class
interface, or separately in an implementation file. How do
you decide?
Readability.
Only put very small methods in the class definition, so that
it is easier to read.
Semantics. Methods defined in class interface may (at the
descretion of the compiler) be expanded in-line.
Another reason for only defining very small methods this
way.
26. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
PREPARING FOR CHANGE I
An interface is like a class, but it provides no
implementation.
Later, another class can declare that it supports the
interface, and it must then give an implementation.
AN INTERFACE IN JAVA
public interface Storing {
void writeOut ( Stream s ) ;
void readFrom ( Stream s ) ;
} ;
public class BitImage implements Storing {
void writeOut ( Stream s ) {
/ / . . .
}
void readFrom ( Stream s ) {
/ / . . .
}
} ;
27. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
PREPARING FOR CHANGE II
We will have much more to say about interfaces later after we
discuss inheritance.
28. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
PROPERTIES I
Properties are a way to define getters and setters, but
allow them to be used as if they were simple assignments
and expressions:
PROPERTIES
w r i t e l n ( ’ rank i s ’ , aCard . rank ) ; (∗ rank i s property of card ∗)
aCard . rank = 5; (∗ changing the rank property ∗)
29. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
PROPERTIES II
PROPERTIES IN C-SHARP
public class PlayingCard {
public int rank {
get
{
return rankValue ;
}
set
{
rankValue = value ;
}
}
. . .
private int rankValue ;
}
Omitting a set makes it read-only, omitting a get makes it
write-only.
30. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
SOFTWARE COMPONENTS I
Some languages (C++ or Java) allow a class definition to
be given inside another class definition.
Whether the inner class can access features of the outer
class is different in different languages.
INNER CLASSES IN JAVA
class LinkedList {
. . .
private class Link { / / inner class
public int value ;
public Link next ;
}
}
31. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
CLASSE DATA FIELDS I
Idea is that all instances of a class can share a common
data field. Simple idea, but how to resolve the following
paradox. All instances have the same behavior:
Either they all initialize the common area, which seems
bad, or
Nobody initializes the common area, which is also bad.
Different languages use a variety of mechanisms to get
around this. See text for details.
32. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
SUMMARY I
In this chapter we have examined the static, or compile
time features of classes:
The syntax used for class definition.
The meaning of visibility modifiers (public and private).
The syntax used for method definition.
Accessor or getter methods, and mutator or setter
methods.
Variations on class themes.
Interfaces.
Properties.
Nested classes.
Class data fields.
33. Introduction Encapsulation and Instantiation Class Definitions Methods Interfaces in Java Properties Inner or Nested
REFERENCES I
Images and content for developing these slides have been
taken from the follwoing book.
An Introduction to Object Oriented Programming, Timothy
Budd.
This presentation is developed using Beamer:
Frankfurt, spruce.