SlideShare une entreprise Scribd logo
1  sur  33
Télécharger pour lire hors ligne
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.

Contenu connexe

Tendances

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
 

Tendances (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
 

En vedette

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
 

En vedette (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
 

Similaire à 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
 

Similaire à 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
 

Plus de adil raja

Plus de 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...
 

Dernier

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Dernier (20)

Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 

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.