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++)
PPTX
Inheritance in oops
PPTX
Friend function & friend class
PPTX
Object as function argument , friend and static function by shahzad younas
PPTX
Friend Function
PDF
Multiple Inheritance
PDF
OOP Inheritance
PPTX
Friend functions
friend function(c++)
Inheritance in oops
Friend function & friend class
Object as function argument , friend and static function by shahzad younas
Friend Function
Multiple Inheritance
OOP Inheritance
Friend functions

What's hot (15)

PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
PPT
Friends function and_classes
PPTX
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
PPT
Inheritance OOP Concept in C++.
PPTX
Inheritance,constructor,friend function
PPT
Inheritance, Object Oriented Programming
PDF
Inheritance
PDF
Access specifiers (Public Private Protected) C++
PPTX
[OOP - Lec 07] Access Specifiers
PPT
Inheritance C#
PDF
Friend function in c++
PPTX
Inheritance ppt
PPT
Java inheritance
PPTX
Inheritance
PDF
Implementation of oop concept in c++
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Friends function and_classes
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Inheritance OOP Concept in C++.
Inheritance,constructor,friend function
Inheritance, Object Oriented Programming
Inheritance
Access specifiers (Public Private Protected) C++
[OOP - Lec 07] Access Specifiers
Inheritance C#
Friend function in c++
Inheritance ppt
Java inheritance
Inheritance
Implementation of oop concept in c++
Ad

Viewers also liked (19)

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

Similar to Poo java (20)

PPT
Class 7 - PHP Object Oriented Programming
PPT
Overview of Object Oriented Programming using C++
PPTX
oop lecture 3
PPT
Classes, objects and methods
PPTX
L04 Software Design 2
PPTX
Interface
PDF
Object-oriented Basics
PPT
C++ classes
PDF
Demystifying Object-Oriented Programming - PHP UK Conference 2017
PPT
01-introduction OOPS concepts in C++ JAVA
PPT
01-introductionto Object ooriented Programming in JAVA CS.ppt
PPTX
CSharp presentation and software developement
PDF
Lab 4 (1).pdf
PPT
Mca 2nd sem u-2 classes & objects
PPT
Jedi slides 2.1 object-oriented concepts
PPTX
Intro to object oriented programming
PDF
Java OOP Programming language (Part 6) - Abstract Class & Interface
PPTX
Better Understanding OOP using C#
PPT
Bca 2nd sem u-2 classes & objects
Class 7 - PHP Object Oriented Programming
Overview of Object Oriented Programming using C++
oop lecture 3
Classes, objects and methods
L04 Software Design 2
Interface
Object-oriented Basics
C++ classes
Demystifying Object-Oriented Programming - PHP UK Conference 2017
01-introduction OOPS concepts in C++ JAVA
01-introductionto Object ooriented Programming in JAVA CS.ppt
CSharp presentation and software developement
Lab 4 (1).pdf
Mca 2nd sem u-2 classes & objects
Jedi slides 2.1 object-oriented concepts
Intro to object oriented programming
Java OOP Programming language (Part 6) - Abstract Class & Interface
Better Understanding OOP using C#
Bca 2nd sem u-2 classes & objects

More from Tony Nguyen (20)

PPTX
Object oriented analysis
PPTX
Directory based cache coherence
PPTX
Business analytics and data mining
PPTX
Big picture of data mining
PPTX
Data mining and knowledge discovery
PPTX
Cache recap
PPTX
How analysis services caching works
PPTX
Hardware managed cache
PPT
Abstract data types
PPTX
Optimizing shared caches in chip multiprocessors
PPT
Abstract class
PPTX
Abstraction file
PPTX
Object model
PPTX
Concurrency with java
PPTX
Data structures and algorithms
PPTX
Inheritance
PPTX
Object oriented programming-with_java
PPTX
Cobol, lisp, and python
PPTX
Extending burp with python
PPTX
Api crash
Object oriented analysis
Directory based cache coherence
Business analytics and data mining
Big picture of data mining
Data mining and knowledge discovery
Cache recap
How analysis services caching works
Hardware managed cache
Abstract data types
Optimizing shared caches in chip multiprocessors
Abstract class
Abstraction file
Object model
Concurrency with java
Data structures and algorithms
Inheritance
Object oriented programming-with_java
Cobol, lisp, and python
Extending burp with python
Api crash

Recently uploaded (20)

PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Encapsulation theory and applications.pdf
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
A Presentation on Artificial Intelligence
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
1. Introduction to Computer Programming.pptx
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Hybrid model detection and classification of lung cancer
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Encapsulation theory and applications.pdf
Heart disease approach using modified random forest and particle swarm optimi...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Hindi spoken digit analysis for native and non-native speakers
A novel scalable deep ensemble learning framework for big data classification...
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
A Presentation on Artificial Intelligence
Programs and apps: productivity, graphics, security and other tools
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
1. Introduction to Computer Programming.pptx
Univ-Connecticut-ChatGPT-Presentaion.pdf
Hybrid model detection and classification of lung cancer
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
NewMind AI Weekly Chronicles - August'25-Week II
cloud_computing_Infrastucture_as_cloud_p
WOOl fibre morphology and structure.pdf for textiles
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
gpt5_lecture_notes_comprehensive_20250812015547.pdf

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/