SlideShare une entreprise Scribd logo
1  sur  34
Abstraction, Inheritance, and
Polymorphism
in Java
“Object Orientation involving encapsulation,
inheritance, polymorphism, and abstraction, is an
important approach in programming and program
design. It is widely accepted and used in industry and
is growing in popularity in the first and second
college-level programming courses.”
Some other reasons to move on
to Java:
• Platform-independent software
• Relatively easy graphics and GUI
programming
• Lots of library packages
• Free compiler and IDEs
Some other reasons to move on
to Java:
• Colleges are teaching it
• Companies are using it
• Students want it
• (Teachers welcome it... ;)
• (Programmers drink it... :)
What are OOP’s claims to fame?
• Better suited for team development
• Facilitates utilizing and creating reusable
software components
• Easier GUI programming
• Easier program maintenance
OOP in a Nutshell:
• A program models a
world of interacting
objects
• Objects create other
objects and “send
messages” to each other
(in Java, call each
other’s methods)
• Each object belongs to a
class; a class defines
properties of its objects
• A class implements an
ADT; the data type of an
object is its class
• Programmers write classes
(and reuse existing classes)
OOP
Case Study: Dance Studio
Quiz:
How many classes we wrote
for this applet?
A. 1
B. 2
C. 5
D. 10
E. 17
DanceStudio
DanceModel
Music
DanceFloor
Controller
Model
View
MaleDancer
FemaleDancer
MaleFoot
FemaleFoot
Dancer
Foot
Good news:
The classes are fairly short
DanceStudio 92 lines MaleDancer 10 lines
DanceModel 50 lines FemaleDancer 10 lines
DanceFloor 30 lines Foot 100 lines
Music 52 lines MaleFoot 42 lines
Dancer 80 lines FemaleFoot 42 lines
• In OOP, the number of classes is not
considered a problem
In a project with 10 classes we need an IDE...
Abstraction
... relevant to the given project (with an
eye to future reuse in similar projects).
Abstraction means ignoring irrelevant
features, properties, or functions and
emphasizing the relevant ones...
“Relevant” to what?
Abstraction
MaleDancer FemaleDancer
Dancer
Encapsulation
Encapsulation means that all data members
(fields) of a class are declared private. Some
methods may be private, too.
The class interacts with other classes (called
the clients of this class) only through the
class’s constructors and public methods.
Constructors and public methods of a class
serve as the interface to class’s clients.
Encapsulation
MaleFoot FemaleFoot
Foot
public abstract class Foot
{
private static final int footWidth = 24;
private boolean amLeft;
private int myX, myY;
private int myDir;
private boolean myWeight;
// Constructor:
protected Foot(String side, int x, int y, int dir)
{
amLeft = side.equals("left");
myX = x;
myY = y;
myDir = dir;
myWeight = true;
}
Continued 
All fields are
private
Encapsulation ensures that
structural changes remain local
• Changes in the code create software
maintenance problems
• Usually, the structure of a class (as defined
by its fields) changes more often than the
class’s constructors and methods
• Encapsulation ensures that when fields
change, no changes are needed in other
classes (a principle known as “locality”)
True or False? Abstraction and
encapsulation are helpful for the
following:
 Team development ________
 Reusable software ________
 GUI programming ________
 Easier program maintenance ________
Answer:
 Team development ________
 Reusable software ________
 GUI programming ________
 Easier program maintenance ________
T
T
T
(True if you are working on system
packages, such as Swing)
F
Inheritance
A class can extend another class,
inheriting all its data members and
methods while redefining some of them
and/or adding its own.
Inheritance represents the is a
relationship between data types. For
example: a FemaleDancer is a
Dancer.
Inheritance Terminology:
public class FemaleDancer extends Dancer
{
...
}
subclass
or
derived class
superclass
or
base class
extends
MaleDancer FemaleDancer
Dancer
Example:
Inheritance (cont’d)
Inheritance (cont’d)
public class FemaleDancer extends Dancer
{
public FemaleDancer(String steps[],
int x, int y, int dir)
{
leftFoot = new FemaleFoot("left", x, y, dir);
rightFoot = new FemaleFoot("right", x, y, dir);
leftFoot.move(-Foot.getWidth() / 2, 0);
rightFoot.move(Foot.getWidth() / 2, 0);
}
}
Constructors are not inherited. The
FemaleDancer class only adds a constructor:
MaleFoot FemaleFoot
Foot
Example:
Inheritance (cont’d)
public class FemaleFoot extends Foot
{
public FemaleFoot(String side, int x, int y, int dir)
{
super(side, x, y, dir); // calls Foot's constructor
}
//
public void drawLeft(Graphics g)
{
...
}
public void drawRight(Graphics g)
{
...
}
}
Supplies methods
that are abstract
in Foot:
Inheritance may be used to define a
hierarchy of classes in an application:
MaleFoot FemaleFoot
Foot
MaleLeftFoot MaleRightFoot FemaleLeftFoot FemaleRightFoot
Object
• You don’t need to have the source code of
a class to extend it
All methods of the base library
class are available in your
derived class
True or False? Inheritance is helpful for
the following:
 Team development ________
 Reusable software ________
 GUI programming ________
 Easier program maintenance ________
Answer:
 Team development ________
 Reusable software ________
 GUI programming ________
 Easier program maintenance ________
F
T
???
T
Polymorphism
Polymorphism ensures that the
appropriate method is called for an
object of a specific type when the object
is disguised as a more general type.
Good news: polymorphism is already
supported in Java — all you have to do
is use it properly.
Polymorphism (cont’d)
Situation 1:
A collection (array, list, etc.) contains
objects of different but related types, all
derived from the same common base
class.
public abstract class Foot
{
...
public void draw(Graphics g)
{
...
if (isLeft())
drawLeft(g);
else
drawRight(g);
...
}
}
Polymorphism replaces old-fashioned use
of explicit object attributes and if-else
(or switch) statements, as in:
These slides and the Dance Studio code are
posted at:
http://www.skylit.com/oop/

Contenu connexe

Tendances

Java interfaces
Java interfacesJava interfaces
Java interfacesjehan1987
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces Tuan Ngo
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classesAnup Burange
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceOum Saokosal
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & InterfaceLinh Lê
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singhdheeraj_cse
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentationtigerwarn
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 

Tendances (15)

Java interfaces
Java interfacesJava interfaces
Java interfaces
 
javainterface
javainterfacejavainterface
javainterface
 
Abstraction
AbstractionAbstraction
Abstraction
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and Interface
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & Interface
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
Abstract classes and interfaces
Abstract classes and interfacesAbstract classes and interfaces
Abstract classes and interfaces
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Abstract method
Abstract methodAbstract method
Abstract method
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 

En vedette

creativity at work fall2015
creativity at work fall2015creativity at work fall2015
creativity at work fall2015Terry Chong
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherenceHarry Potter
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsJames Wong
 
Rafael orsini 24118492
Rafael orsini 24118492Rafael orsini 24118492
Rafael orsini 24118492orsini07
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsTony Nguyen
 
Búsqueda no informada - Backtracking/Hacia atrás
Búsqueda no informada - Backtracking/Hacia atrásBúsqueda no informada - Backtracking/Hacia atrás
Búsqueda no informada - Backtracking/Hacia atrásLaura Del Pino Díaz
 
Prolog Visualizer
Prolog VisualizerProlog Visualizer
Prolog VisualizerZhixuan Lai
 
Power point 1 η μικροδιδασκαλία
Power point 1 η μικροδιδασκαλίαPower point 1 η μικροδιδασκαλία
Power point 1 η μικροδιδασκαλίαMaria Antorka
 
Εξεταστέα ύλη μαθημάτων της Γ΄τάξης του Τομέα Δομικών Εργων σχ.έτους 2015-16
Εξεταστέα ύλη μαθημάτων της Γ΄τάξης του Τομέα Δομικών Εργων σχ.έτους 2015-16Εξεταστέα ύλη μαθημάτων της Γ΄τάξης του Τομέα Δομικών Εργων σχ.έτους 2015-16
Εξεταστέα ύλη μαθημάτων της Γ΄τάξης του Τομέα Δομικών Εργων σχ.έτους 2015-16John Tzortzakis
 
Αναλυτικά Προγράμματα Σπουδών Β ́ και Γ ́ τάξεων Τομέα Δομικών Έργων ΦΕΚ 770 ...
Αναλυτικά Προγράμματα Σπουδών Β ́ και Γ ́ τάξεων Τομέα Δομικών Έργων ΦΕΚ 770 ...Αναλυτικά Προγράμματα Σπουδών Β ́ και Γ ́ τάξεων Τομέα Δομικών Έργων ΦΕΚ 770 ...
Αναλυτικά Προγράμματα Σπουδών Β ́ και Γ ́ τάξεων Τομέα Δομικών Έργων ΦΕΚ 770 ...John Tzortzakis
 

En vedette (15)

creativity at work fall2015
creativity at work fall2015creativity at work fall2015
creativity at work fall2015
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherence
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessors
 
Rafael orsini 24118492
Rafael orsini 24118492Rafael orsini 24118492
Rafael orsini 24118492
 
ΛΟΙΜΩΞΕΙΣ
ΛΟΙΜΩΞΕΙΣΛΟΙΜΩΞΕΙΣ
ΛΟΙΜΩΞΕΙΣ
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Búsqueda no informada - Backtracking/Hacia atrás
Búsqueda no informada - Backtracking/Hacia atrásBúsqueda no informada - Backtracking/Hacia atrás
Búsqueda no informada - Backtracking/Hacia atrás
 
Busqueda informada y explorada
Busqueda informada y exploradaBusqueda informada y explorada
Busqueda informada y explorada
 
Abstract class
Abstract classAbstract class
Abstract class
 
Internet de las cosas
Internet de las cosasInternet de las cosas
Internet de las cosas
 
Prolog Visualizer
Prolog VisualizerProlog Visualizer
Prolog Visualizer
 
Power point 1 η μικροδιδασκαλία
Power point 1 η μικροδιδασκαλίαPower point 1 η μικροδιδασκαλία
Power point 1 η μικροδιδασκαλία
 
αγαθα διακρισεις αγαθων
αγαθα διακρισεις αγαθωναγαθα διακρισεις αγαθων
αγαθα διακρισεις αγαθων
 
Εξεταστέα ύλη μαθημάτων της Γ΄τάξης του Τομέα Δομικών Εργων σχ.έτους 2015-16
Εξεταστέα ύλη μαθημάτων της Γ΄τάξης του Τομέα Δομικών Εργων σχ.έτους 2015-16Εξεταστέα ύλη μαθημάτων της Γ΄τάξης του Τομέα Δομικών Εργων σχ.έτους 2015-16
Εξεταστέα ύλη μαθημάτων της Γ΄τάξης του Τομέα Δομικών Εργων σχ.έτους 2015-16
 
Αναλυτικά Προγράμματα Σπουδών Β ́ και Γ ́ τάξεων Τομέα Δομικών Έργων ΦΕΚ 770 ...
Αναλυτικά Προγράμματα Σπουδών Β ́ και Γ ́ τάξεων Τομέα Δομικών Έργων ΦΕΚ 770 ...Αναλυτικά Προγράμματα Σπουδών Β ́ και Γ ́ τάξεων Τομέα Δομικών Έργων ΦΕΚ 770 ...
Αναλυτικά Προγράμματα Σπουδών Β ́ και Γ ́ τάξεων Τομέα Δομικών Έργων ΦΕΚ 770 ...
 

Similaire à OOP Concepts in Java: Abstraction, Inheritance and Polymorphism

Similaire à OOP Concepts in Java: Abstraction, Inheritance and Polymorphism (20)

Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
 
Interface
InterfaceInterface
Interface
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
C++ classes
C++ classesC++ classes
C++ classes
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
 
Lab 4 (1).pdf
Lab 4 (1).pdfLab 4 (1).pdf
Lab 4 (1).pdf
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
Intro to object oriented programming
Intro to object oriented programmingIntro to object oriented programming
Intro to object oriented programming
 
Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
Object
ObjectObject
Object
 
oopusingc.pptx
oopusingc.pptxoopusingc.pptx
oopusingc.pptx
 
Oops in java
Oops in javaOops in java
Oops in java
 
Oops concept
Oops conceptOops concept
Oops concept
 
oops-1
oops-1oops-1
oops-1
 

Plus de Fraboni Ec

Hardware multithreading
Hardware multithreadingHardware multithreading
Hardware multithreadingFraboni Ec
 
What is simultaneous multithreading
What is simultaneous multithreadingWhat is simultaneous multithreading
What is simultaneous multithreadingFraboni Ec
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherenceFraboni Ec
 
Business analytics and data mining
Business analytics and data miningBusiness analytics and data mining
Business analytics and data miningFraboni Ec
 
Big picture of data mining
Big picture of data miningBig picture of data mining
Big picture of data miningFraboni Ec
 
Data mining and knowledge discovery
Data mining and knowledge discoveryData mining and knowledge discovery
Data mining and knowledge discoveryFraboni Ec
 
How analysis services caching works
How analysis services caching worksHow analysis services caching works
How analysis services caching worksFraboni Ec
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cacheFraboni Ec
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsFraboni Ec
 
Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and pythonFraboni Ec
 
Abstract data types
Abstract data typesAbstract data types
Abstract data typesFraboni Ec
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsFraboni Ec
 
Abstraction file
Abstraction fileAbstraction file
Abstraction fileFraboni Ec
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisFraboni Ec
 
Abstract class
Abstract classAbstract class
Abstract classFraboni Ec
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaFraboni Ec
 

Plus de Fraboni Ec (20)

Hardware multithreading
Hardware multithreadingHardware multithreading
Hardware multithreading
 
Lisp
LispLisp
Lisp
 
What is simultaneous multithreading
What is simultaneous multithreadingWhat is simultaneous multithreading
What is simultaneous multithreading
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherence
 
Business analytics and data mining
Business analytics and data miningBusiness analytics and data mining
Business analytics and data mining
 
Big picture of data mining
Big picture of data miningBig picture of data mining
Big picture of data mining
 
Data mining and knowledge discovery
Data mining and knowledge discoveryData mining and knowledge discovery
Data mining and knowledge discovery
 
Cache recap
Cache recapCache recap
Cache recap
 
How analysis services caching works
How analysis services caching worksHow analysis services caching works
How analysis services caching works
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cache
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and python
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessors
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
Object model
Object modelObject model
Object model
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Abstract class
Abstract classAbstract class
Abstract class
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Inheritance
InheritanceInheritance
Inheritance
 

Dernier

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 

Dernier (20)

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 

OOP Concepts in Java: Abstraction, Inheritance and Polymorphism

  • 2. “Object Orientation involving encapsulation, inheritance, polymorphism, and abstraction, is an important approach in programming and program design. It is widely accepted and used in industry and is growing in popularity in the first and second college-level programming courses.”
  • 3. Some other reasons to move on to Java: • Platform-independent software • Relatively easy graphics and GUI programming • Lots of library packages • Free compiler and IDEs
  • 4. Some other reasons to move on to Java: • Colleges are teaching it • Companies are using it • Students want it • (Teachers welcome it... ;) • (Programmers drink it... :)
  • 5. What are OOP’s claims to fame? • Better suited for team development • Facilitates utilizing and creating reusable software components • Easier GUI programming • Easier program maintenance
  • 6. OOP in a Nutshell: • A program models a world of interacting objects • Objects create other objects and “send messages” to each other (in Java, call each other’s methods) • Each object belongs to a class; a class defines properties of its objects • A class implements an ADT; the data type of an object is its class • Programmers write classes (and reuse existing classes)
  • 7. OOP
  • 9. Quiz: How many classes we wrote for this applet? A. 1 B. 2 C. 5 D. 10 E. 17
  • 11. Good news: The classes are fairly short DanceStudio 92 lines MaleDancer 10 lines DanceModel 50 lines FemaleDancer 10 lines DanceFloor 30 lines Foot 100 lines Music 52 lines MaleFoot 42 lines Dancer 80 lines FemaleFoot 42 lines • In OOP, the number of classes is not considered a problem
  • 12. In a project with 10 classes we need an IDE...
  • 13. Abstraction ... relevant to the given project (with an eye to future reuse in similar projects). Abstraction means ignoring irrelevant features, properties, or functions and emphasizing the relevant ones... “Relevant” to what?
  • 15. Encapsulation Encapsulation means that all data members (fields) of a class are declared private. Some methods may be private, too. The class interacts with other classes (called the clients of this class) only through the class’s constructors and public methods. Constructors and public methods of a class serve as the interface to class’s clients.
  • 17. public abstract class Foot { private static final int footWidth = 24; private boolean amLeft; private int myX, myY; private int myDir; private boolean myWeight; // Constructor: protected Foot(String side, int x, int y, int dir) { amLeft = side.equals("left"); myX = x; myY = y; myDir = dir; myWeight = true; } Continued  All fields are private
  • 18. Encapsulation ensures that structural changes remain local • Changes in the code create software maintenance problems • Usually, the structure of a class (as defined by its fields) changes more often than the class’s constructors and methods • Encapsulation ensures that when fields change, no changes are needed in other classes (a principle known as “locality”)
  • 19. True or False? Abstraction and encapsulation are helpful for the following:  Team development ________  Reusable software ________  GUI programming ________  Easier program maintenance ________
  • 20. Answer:  Team development ________  Reusable software ________  GUI programming ________  Easier program maintenance ________ T T T (True if you are working on system packages, such as Swing) F
  • 21. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own. Inheritance represents the is a relationship between data types. For example: a FemaleDancer is a Dancer.
  • 22. Inheritance Terminology: public class FemaleDancer extends Dancer { ... } subclass or derived class superclass or base class extends
  • 24. Inheritance (cont’d) public class FemaleDancer extends Dancer { public FemaleDancer(String steps[], int x, int y, int dir) { leftFoot = new FemaleFoot("left", x, y, dir); rightFoot = new FemaleFoot("right", x, y, dir); leftFoot.move(-Foot.getWidth() / 2, 0); rightFoot.move(Foot.getWidth() / 2, 0); } } Constructors are not inherited. The FemaleDancer class only adds a constructor:
  • 26. public class FemaleFoot extends Foot { public FemaleFoot(String side, int x, int y, int dir) { super(side, x, y, dir); // calls Foot's constructor } // public void drawLeft(Graphics g) { ... } public void drawRight(Graphics g) { ... } } Supplies methods that are abstract in Foot:
  • 27. Inheritance may be used to define a hierarchy of classes in an application: MaleFoot FemaleFoot Foot MaleLeftFoot MaleRightFoot FemaleLeftFoot FemaleRightFoot Object
  • 28. • You don’t need to have the source code of a class to extend it All methods of the base library class are available in your derived class
  • 29. True or False? Inheritance is helpful for the following:  Team development ________  Reusable software ________  GUI programming ________  Easier program maintenance ________
  • 30. Answer:  Team development ________  Reusable software ________  GUI programming ________  Easier program maintenance ________ F T ??? T
  • 31. Polymorphism Polymorphism ensures that the appropriate method is called for an object of a specific type when the object is disguised as a more general type. Good news: polymorphism is already supported in Java — all you have to do is use it properly.
  • 32. Polymorphism (cont’d) Situation 1: A collection (array, list, etc.) contains objects of different but related types, all derived from the same common base class.
  • 33. public abstract class Foot { ... public void draw(Graphics g) { ... if (isLeft()) drawLeft(g); else drawRight(g); ... } } Polymorphism replaces old-fashioned use of explicit object attributes and if-else (or switch) statements, as in:
  • 34. These slides and the Dance Studio code are posted at: http://www.skylit.com/oop/