SlideShare a Scribd company logo
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/

More Related Content

PPT
friend function(c++)
Ritika Sharma
 
PPTX
Inheritance in oops
Hirra Sultan
 
PPTX
Friend function & friend class
Abhishek Wadhwa
 
PPTX
Object as function argument , friend and static function by shahzad younas
Shahzad Younas
 
PPTX
Friend Function
Mehak Tawakley
 
PDF
Multiple Inheritance
adil raja
 
PDF
OOP Inheritance
Anastasia Jakubow
 
PPTX
Friend functions
Megha Singh
 
friend function(c++)
Ritika Sharma
 
Inheritance in oops
Hirra Sultan
 
Friend function & friend class
Abhishek Wadhwa
 
Object as function argument , friend and static function by shahzad younas
Shahzad Younas
 
Friend Function
Mehak Tawakley
 
Multiple Inheritance
adil raja
 
OOP Inheritance
Anastasia Jakubow
 
Friend functions
Megha Singh
 

What's hot (15)

PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
cprogrammings
 
PPT
Friends function and_classes
asadsardar
 
PPTX
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
PPT
Inheritance OOP Concept in C++.
MASQ Technologies
 
PPTX
Inheritance,constructor,friend function
FAKRUL ISLAM
 
PPT
Inheritance, Object Oriented Programming
Arslan Waseem
 
PDF
Inheritance
Padma Kannan
 
PDF
Access specifiers (Public Private Protected) C++
vivekkumar2938
 
PPTX
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
PPT
Inheritance C#
Raghuveer Guthikonda
 
PDF
Friend function in c++
University of Madras
 
PPTX
Inheritance ppt
Nivegeetha
 
PPT
Java inheritance
Arati Gadgil
 
PPTX
Inheritance
prabhat kumar
 
PDF
Implementation of oop concept in c++
Swarup Kumar Boro
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
cprogrammings
 
Friends function and_classes
asadsardar
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
Inheritance OOP Concept in C++.
MASQ Technologies
 
Inheritance,constructor,friend function
FAKRUL ISLAM
 
Inheritance, Object Oriented Programming
Arslan Waseem
 
Inheritance
Padma Kannan
 
Access specifiers (Public Private Protected) C++
vivekkumar2938
 
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
Inheritance C#
Raghuveer Guthikonda
 
Friend function in c++
University of Madras
 
Inheritance ppt
Nivegeetha
 
Java inheritance
Arati Gadgil
 
Inheritance
prabhat kumar
 
Implementation of oop concept in c++
Swarup Kumar Boro
 
Ad

Viewers also liked (9)

PPTX
Constructors
shravani2191
 
PPTX
Encapsulation getters setters_anonymousclass
Hoang Nguyen
 
PPT
Chapter 5 Class File
Khirulnizam Abd Rahman
 
PPTX
Week 6 java script loops
brianjihoonlee
 
PPT
Chapter 2 Java Methods
Khirulnizam Abd Rahman
 
PPT
Java Programming: Loops
Karwan Mustafa Kareem
 
PPT
06 abstract-classes
Anup Burange
 
PDF
Operators in java
Ravi_Kant_Sahu
 
PDF
Methods in Java
Jussi Pohjolainen
 
Constructors
shravani2191
 
Encapsulation getters setters_anonymousclass
Hoang Nguyen
 
Chapter 5 Class File
Khirulnizam Abd Rahman
 
Week 6 java script loops
brianjihoonlee
 
Chapter 2 Java Methods
Khirulnizam Abd Rahman
 
Java Programming: Loops
Karwan Mustafa Kareem
 
06 abstract-classes
Anup Burange
 
Operators in java
Ravi_Kant_Sahu
 
Methods in Java
Jussi Pohjolainen
 
Ad

Similar to Poo java (20)

PPT
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
PPT
Overview of Object Oriented Programming using C++
jayanthi699330
 
PPTX
oop lecture 3
Atif Khan
 
PPT
Classes, objects and methods
farhan amjad
 
PPTX
L04 Software Design 2
Ólafur Andri Ragnarsson
 
PPTX
Interface
Ashwini Yadav
 
PDF
Object-oriented Basics
Jamie (Taka) Wang
 
PPT
C++ classes
imhammadali
 
PDF
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Alena Holligan
 
PPT
01-introduction OOPS concepts in C++ JAVA
muraliravisubs
 
PPT
01-introductionto Object ooriented Programming in JAVA CS.ppt
GESISLAMIAPATTOKI
 
PPTX
CSharp presentation and software developement
frwebhelp
 
PDF
Lab 4 (1).pdf
MohammedAlobaidy16
 
PPT
Mca 2nd sem u-2 classes & objects
Rai University
 
PPT
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
PPTX
Intro to object oriented programming
David Giard
 
PDF
Java OOP Programming language (Part 6) - Abstract Class & Interface
OUM SAOKOSAL
 
PPTX
Better Understanding OOP using C#
Chandan Gupta Bhagat
 
PPT
Bca 2nd sem u-2 classes & objects
Rai University
 
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Overview of Object Oriented Programming using C++
jayanthi699330
 
oop lecture 3
Atif Khan
 
Classes, objects and methods
farhan amjad
 
L04 Software Design 2
Ólafur Andri Ragnarsson
 
Interface
Ashwini Yadav
 
Object-oriented Basics
Jamie (Taka) Wang
 
C++ classes
imhammadali
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Alena Holligan
 
01-introduction OOPS concepts in C++ JAVA
muraliravisubs
 
01-introductionto Object ooriented Programming in JAVA CS.ppt
GESISLAMIAPATTOKI
 
CSharp presentation and software developement
frwebhelp
 
Lab 4 (1).pdf
MohammedAlobaidy16
 
Mca 2nd sem u-2 classes & objects
Rai University
 
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
Intro to object oriented programming
David Giard
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
OUM SAOKOSAL
 
Better Understanding OOP using C#
Chandan Gupta Bhagat
 
Bca 2nd sem u-2 classes & objects
Rai University
 

More from James Wong (20)

PPT
Data race
James Wong
 
PPT
Multi threaded rtos
James Wong
 
PPT
Recursion
James Wong
 
PPTX
Business analytics and data mining
James Wong
 
PPTX
Data mining and knowledge discovery
James Wong
 
PPTX
Cache recap
James Wong
 
PPTX
Big picture of data mining
James Wong
 
PPTX
How analysis services caching works
James Wong
 
PPTX
Optimizing shared caches in chip multiprocessors
James Wong
 
PPTX
Directory based cache coherence
James Wong
 
PPT
Abstract data types
James Wong
 
PPTX
Abstraction file
James Wong
 
PPTX
Hardware managed cache
James Wong
 
PPTX
Object model
James Wong
 
PPT
Abstract class
James Wong
 
PPTX
Object oriented analysis
James Wong
 
PPTX
Concurrency with java
James Wong
 
PPTX
Data structures and algorithms
James Wong
 
PPTX
Cobol, lisp, and python
James Wong
 
PPTX
Inheritance
James Wong
 
Data race
James Wong
 
Multi threaded rtos
James Wong
 
Recursion
James Wong
 
Business analytics and data mining
James Wong
 
Data mining and knowledge discovery
James Wong
 
Cache recap
James Wong
 
Big picture of data mining
James Wong
 
How analysis services caching works
James Wong
 
Optimizing shared caches in chip multiprocessors
James Wong
 
Directory based cache coherence
James Wong
 
Abstract data types
James Wong
 
Abstraction file
James Wong
 
Hardware managed cache
James Wong
 
Object model
James Wong
 
Abstract class
James Wong
 
Object oriented analysis
James Wong
 
Concurrency with java
James Wong
 
Data structures and algorithms
James Wong
 
Cobol, lisp, and python
James Wong
 
Inheritance
James Wong
 

Recently uploaded (20)

PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Doc9.....................................
SofiaCollazos
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
The Future of Artificial Intelligence (AI)
Mukul
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Doc9.....................................
SofiaCollazos
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 

Poo java

  • 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/