SlideShare ist ein Scribd-Unternehmen logo
1 von 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/

Weitere ähnliche Inhalte

Was ist angesagt?

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...cprogrammings
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classesasadsardar
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...Simplilearn
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.MASQ Technologies
 
Inheritance,constructor,friend function
Inheritance,constructor,friend functionInheritance,constructor,friend function
Inheritance,constructor,friend functionFAKRUL ISLAM
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingArslan Waseem
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++vivekkumar2938
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
 
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
 

Was ist angesagt? (15)

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...
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Inheritance,constructor,friend function
Inheritance,constructor,friend functionInheritance,constructor,friend function
Inheritance,constructor,friend function
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Inheritance
InheritanceInheritance
Inheritance
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
 
Friend function in c++
Friend function in c++ Friend function in c++
Friend function in c++
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 

Andere mochten auch

Abstract data types
Abstract data typesAbstract data types
Abstract data typesHarry Potter
 
Programming for engineers in python
Programming for engineers in pythonProgramming for engineers in python
Programming for engineers in pythonHarry Potter
 
How to Size a Condensate Return Unit
How to Size a Condensate Return UnitHow to Size a Condensate Return Unit
How to Size a Condensate Return UnitKevin Doyle
 
2015 bioinformatics bio_cheminformatics_wim_vancriekinge
2015 bioinformatics bio_cheminformatics_wim_vancriekinge2015 bioinformatics bio_cheminformatics_wim_vancriekinge
2015 bioinformatics bio_cheminformatics_wim_vancriekingeProf. Wim Van Criekinge
 
Ado.net & data persistence frameworks
Ado.net & data persistence frameworksAdo.net & data persistence frameworks
Ado.net & data persistence frameworksLuis Goldster
 
GRAFICA, DOMINIO Y RANGO DE UNA FUNCIÓN
GRAFICA, DOMINIO Y RANGO DE UNA FUNCIÓNGRAFICA, DOMINIO Y RANGO DE UNA FUNCIÓN
GRAFICA, DOMINIO Y RANGO DE UNA FUNCIÓNinnovalabcun
 
X.500 More Than a Global Directory
X.500 More Than a Global DirectoryX.500 More Than a Global Directory
X.500 More Than a Global Directorylurdhu agnes
 
Data mining and knowledge discovery
Data mining and knowledge discoveryData mining and knowledge discovery
Data mining and knowledge discoveryLuis Goldster
 
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
2015 bioinformatics python_introduction_wim_vancriekinge_vfinalProf. Wim Van Criekinge
 
Ρευστά σε κίνηση
Ρευστά σε κίνησηΡευστά σε κίνηση
Ρευστά σε κίνησηGiannis Stathis
 
The Ldap Protocol
The Ldap ProtocolThe Ldap Protocol
The Ldap ProtocolGlen Plantz
 
Computer organization memory hierarchy
Computer organization memory hierarchyComputer organization memory hierarchy
Computer organization memory hierarchyAJAL A J
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisMahesh Bhalerao
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and DesignAnirban Majumdar
 

Andere mochten auch (19)

Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Programming for engineers in python
Programming for engineers in pythonProgramming for engineers in python
Programming for engineers in python
 
How to Size a Condensate Return Unit
How to Size a Condensate Return UnitHow to Size a Condensate Return Unit
How to Size a Condensate Return Unit
 
2016 02 23_biological_databases_part1
2016 02 23_biological_databases_part12016 02 23_biological_databases_part1
2016 02 23_biological_databases_part1
 
2015 bioinformatics bio_cheminformatics_wim_vancriekinge
2015 bioinformatics bio_cheminformatics_wim_vancriekinge2015 bioinformatics bio_cheminformatics_wim_vancriekinge
2015 bioinformatics bio_cheminformatics_wim_vancriekinge
 
Ado.net & data persistence frameworks
Ado.net & data persistence frameworksAdo.net & data persistence frameworks
Ado.net & data persistence frameworks
 
GRAFICA, DOMINIO Y RANGO DE UNA FUNCIÓN
GRAFICA, DOMINIO Y RANGO DE UNA FUNCIÓNGRAFICA, DOMINIO Y RANGO DE UNA FUNCIÓN
GRAFICA, DOMINIO Y RANGO DE UNA FUNCIÓN
 
Memory system
Memory systemMemory system
Memory system
 
X.500 More Than a Global Directory
X.500 More Than a Global DirectoryX.500 More Than a Global Directory
X.500 More Than a Global Directory
 
المادة العلمية محاضرة 2 كيفية كتابة المسح الأدبي
المادة العلمية محاضرة 2 كيفية كتابة المسح الأدبيالمادة العلمية محاضرة 2 كيفية كتابة المسح الأدبي
المادة العلمية محاضرة 2 كيفية كتابة المسح الأدبي
 
DIFERENCIACIÓN
DIFERENCIACIÓN DIFERENCIACIÓN
DIFERENCIACIÓN
 
Data mining and knowledge discovery
Data mining and knowledge discoveryData mining and knowledge discovery
Data mining and knowledge discovery
 
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
 
Ρευστά σε κίνηση
Ρευστά σε κίνησηΡευστά σε κίνηση
Ρευστά σε κίνηση
 
The Ldap Protocol
The Ldap ProtocolThe Ldap Protocol
The Ldap Protocol
 
memory hierarchy
memory hierarchymemory hierarchy
memory hierarchy
 
Computer organization memory hierarchy
Computer organization memory hierarchyComputer organization memory hierarchy
Computer organization memory hierarchy
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and Design
 

Ähnlich wie OOP Concepts in Java: Abstraction, Inheritance and Polymorphism

Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3Atif Khan
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
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 2017Alena Holligan
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developementfrwebhelp
 
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 & objectsRai University
 
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 conceptsMaryo Manjaruni
 
Intro to object oriented programming
Intro to object oriented programmingIntro to object oriented programming
Intro to object oriented programmingDavid Giard
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceOUM SAOKOSAL
 
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 & objectsRai University
 

Ähnlich wie 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
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & Interface
 
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
 

Mehr von Tony Nguyen

Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisTony Nguyen
 
Directory based cache coherence
Directory based cache coherenceDirectory based cache coherence
Directory based cache coherenceTony Nguyen
 
Business analytics and data mining
Business analytics and data miningBusiness analytics and data mining
Business analytics and data miningTony Nguyen
 
Big picture of data mining
Big picture of data miningBig picture of data mining
Big picture of data miningTony Nguyen
 
Data mining and knowledge discovery
Data mining and knowledge discoveryData mining and knowledge discovery
Data mining and knowledge discoveryTony Nguyen
 
How analysis services caching works
How analysis services caching worksHow analysis services caching works
How analysis services caching worksTony Nguyen
 
Hardware managed cache
Hardware managed cacheHardware managed cache
Hardware managed cacheTony Nguyen
 
Abstract data types
Abstract data typesAbstract data types
Abstract data typesTony Nguyen
 
Optimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsOptimizing shared caches in chip multiprocessors
Optimizing shared caches in chip multiprocessorsTony Nguyen
 
Abstraction file
Abstraction fileAbstraction file
Abstraction fileTony Nguyen
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with javaTony Nguyen
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsTony Nguyen
 
Object oriented programming-with_java
Object oriented programming-with_javaObject oriented programming-with_java
Object oriented programming-with_javaTony Nguyen
 
Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and pythonTony Nguyen
 
Extending burp with python
Extending burp with pythonExtending burp with python
Extending burp with pythonTony Nguyen
 

Mehr von Tony Nguyen (20)

Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
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
 
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
 
Abstract class
Abstract classAbstract class
Abstract class
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
Object model
Object modelObject model
Object model
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Inheritance
InheritanceInheritance
Inheritance
 
Object oriented programming-with_java
Object oriented programming-with_javaObject oriented programming-with_java
Object oriented programming-with_java
 
Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and python
 
Extending burp with python
Extending burp with pythonExtending burp with python
Extending burp with python
 
Api crash
Api crashApi crash
Api crash
 

Kürzlich hochgeladen

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 

Kürzlich hochgeladen (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

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/