SlideShare a Scribd company logo
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
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
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.
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
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.
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.
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).
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.
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 ;
} ;
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).
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.
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.
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;
}
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.
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;
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.
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 ;
}
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.
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.
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 ;
}
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.
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.
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 ;
}
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.
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.
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 ) {
/ / . . .
}
} ;
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.
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 ∗)
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.
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 ;
}
}
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.
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.
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.

More Related Content

What's hot

java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
Arjun Shanka
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
Swarup Kumar Boro
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 
2013 bookmatter learn_javaforandroiddevelopment
2013 bookmatter learn_javaforandroiddevelopment2013 bookmatter learn_javaforandroiddevelopment
2013 bookmatter learn_javaforandroiddevelopment
CarlosPineda729332
 

What's hot (20)

java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
C# interview
C# interviewC# interview
C# interview
 
javainterface
javainterfacejavainterface
javainterface
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
 
Abstraction
AbstractionAbstraction
Abstraction
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
inheritance
inheritanceinheritance
inheritance
 
2013 bookmatter learn_javaforandroiddevelopment
2013 bookmatter learn_javaforandroiddevelopment2013 bookmatter learn_javaforandroiddevelopment
2013 bookmatter learn_javaforandroiddevelopment
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Java interface
Java interfaceJava interface
Java interface
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
Class and object
Class and objectClass and object
Class and object
 
Poo java
Poo javaPoo java
Poo java
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and Interface
 

Viewers also liked

12 couplingand cohesion-student
12 couplingand cohesion-student12 couplingand cohesion-student
12 couplingand cohesion-student
randhirlpu
 
Data structure and algorithms in c++
Data structure and algorithms in c++Data structure and algorithms in c++
Data structure and algorithms in c++
Karmjeet Chahal
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphism
Jussi Pohjolainen
 

Viewers also liked (20)

12 couplingand cohesion-student
12 couplingand cohesion-student12 couplingand cohesion-student
12 couplingand cohesion-student
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design pattern
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Polymorphism and Software Reuse
Polymorphism and Software ReusePolymorphism and Software Reuse
Polymorphism and Software Reuse
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
XKE - Programming Paradigms & Constructs
XKE - Programming Paradigms & ConstructsXKE - Programming Paradigms & Constructs
XKE - Programming Paradigms & Constructs
 
The Physical Layer
The Physical LayerThe Physical Layer
The Physical Layer
 
04 design concepts_n_principles
04 design concepts_n_principles04 design concepts_n_principles
04 design concepts_n_principles
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tables
 
Syntax part 6
Syntax part 6Syntax part 6
Syntax part 6
 
The Data Link Layer
The Data Link LayerThe Data Link Layer
The Data Link Layer
 
Universal Declarative Services
Universal Declarative ServicesUniversal Declarative Services
Universal Declarative Services
 
Data structure and algorithms in c++
Data structure and algorithms in c++Data structure and algorithms in c++
Data structure and algorithms in c++
 
Association agggregation and composition
Association agggregation and compositionAssociation agggregation and composition
Association agggregation and composition
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
WSO2 Complex Event Processor
WSO2 Complex Event ProcessorWSO2 Complex Event Processor
WSO2 Complex Event Processor
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphism
 
Cohesion and coherence
Cohesion and coherenceCohesion and coherence
Cohesion and coherence
 
Cohesion & Coupling
Cohesion & Coupling Cohesion & Coupling
Cohesion & Coupling
 
The Network Layer
The Network LayerThe Network Layer
The Network Layer
 

Similar to Classes And Methods

INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
Indu65
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
Zafor Iqbal
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
AdilAijaz3
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
frwebhelp
 

Similar to Classes And Methods (20)

DAY_1.1.pptx
DAY_1.1.pptxDAY_1.1.pptx
DAY_1.1.pptx
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
 
Java & J2EE Coding Conventions
Java & J2EE Coding ConventionsJava & J2EE Coding Conventions
Java & J2EE Coding Conventions
 
Java 06
Java 06Java 06
Java 06
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
Core java
Core javaCore java
Core java
 
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
 
Java assignment help
Java assignment helpJava assignment help
Java assignment help
 
Java
JavaJava
Java
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answers
 

More from adil raja

More from adil raja (20)

ANNs.pdf
ANNs.pdfANNs.pdf
ANNs.pdf
 
A Software Requirements Specification
A Software Requirements SpecificationA Software Requirements Specification
A Software Requirements Specification
 
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial VehiclesNUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
 
DevOps Demystified
DevOps DemystifiedDevOps Demystified
DevOps Demystified
 
On Research (And Development)
On Research (And Development)On Research (And Development)
On Research (And Development)
 
Simulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge ResearchSimulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge Research
 
The Knock Knock Protocol
The Knock Knock ProtocolThe Knock Knock Protocol
The Knock Knock Protocol
 
File Transfer Through Sockets
File Transfer Through SocketsFile Transfer Through Sockets
File Transfer Through Sockets
 
Remote Command Execution
Remote Command ExecutionRemote Command Execution
Remote Command Execution
 
Thesis
ThesisThesis
Thesis
 
CMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor PakistanCMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor Pakistan
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIPReal-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
 
VoIP
VoIPVoIP
VoIP
 
ULMAN GUI Specifications
ULMAN GUI SpecificationsULMAN GUI Specifications
ULMAN GUI Specifications
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 
ULMAN-GUI
ULMAN-GUIULMAN-GUI
ULMAN-GUI
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 

Recently uploaded

Recently uploaded (20)

Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024
 
How to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabberHow to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabber
 
INGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignINGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by Design
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
KLARNA - Language Models and Knowledge Graphs: A Systems Approach
KLARNA -  Language Models and Knowledge Graphs: A Systems ApproachKLARNA -  Language Models and Knowledge Graphs: A Systems Approach
KLARNA - Language Models and Knowledge Graphs: A Systems Approach
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM Integration
 
Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024
 
How To Build a Successful SaaS Design.pdf
How To Build a Successful SaaS Design.pdfHow To Build a Successful SaaS Design.pdf
How To Build a Successful SaaS Design.pdf
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by Skilrock
 
10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
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.