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 (13)

PPSX
java concept
PPTX
Object Oriented Programming Concepts
PDF
javainterface
PPSX
Concept of Object Oriented Programming
PPTX
Interesting Concept of Object Oriented Programming
PPT
Advanced OOP - Laws, Principles, Idioms
PPT
Java Programming - Abstract Class and Interface
PPTX
Java interview questions 2
PDF
02 basic java programming and operators
PPT
20. Object-Oriented Programming Fundamental Principles
PPT
Java OOP s concepts and buzzwords
PPT
Electric circuits
PDF
LinkedIn SlideShare: Knowledge, Well-Presented
java concept
Object Oriented Programming Concepts
javainterface
Concept of Object Oriented Programming
Interesting Concept of Object Oriented Programming
Advanced OOP - Laws, Principles, Idioms
Java Programming - Abstract Class and Interface
Java interview questions 2
02 basic java programming and operators
20. Object-Oriented Programming Fundamental Principles
Java OOP s concepts and buzzwords
Electric circuits
LinkedIn SlideShare: Knowledge, Well-Presented
Ad

Similar to Abstrac tinheritance polymorphism (20)

PDF
Object-Oriented Programming in Java.pdf
PDF
Programming Laboratory Unit 1.pdf
PPTX
Introduction to OOP concepts
PPTX
chapter 5 concepts of object oriented programming
PPTX
oops concept in java | object oriented programming in java
PPTX
Object+oriented+programming+in+java
PPTX
Features of Object Oriented Programming.pptx
PPTX
ITTutor Advanced Java (1).pptx
PDF
Oops concepts
PPTX
java part 1 computer science.pptx
PPSX
Oop features java presentationshow
PPTX
OOPS (Object Oriented Programming System) CONCEPTS
DOCX
Java oop concepts
PPTX
Object Oriented Programming (OOP) Introduction
PPT
Chapter 5 (OOP Principles).ppt
PPTX
Java OOPS Concept
PPTX
SKILLWISE - OOPS CONCEPT
Object-Oriented Programming in Java.pdf
Programming Laboratory Unit 1.pdf
Introduction to OOP concepts
chapter 5 concepts of object oriented programming
oops concept in java | object oriented programming in java
Object+oriented+programming+in+java
Features of Object Oriented Programming.pptx
ITTutor Advanced Java (1).pptx
Oops concepts
java part 1 computer science.pptx
Oop features java presentationshow
OOPS (Object Oriented Programming System) CONCEPTS
Java oop concepts
Object Oriented Programming (OOP) Introduction
Chapter 5 (OOP Principles).ppt
Java OOPS Concept
SKILLWISE - OOPS CONCEPT

More from Hoang Nguyen (20)

PPTX
Rest api to integrate with your site
PPTX
How to build a rest api
PPTX
Api crash
PPTX
Smm and caching
PPTX
Optimizing shared caches in chip multiprocessors
PPTX
How analysis services caching works
PPTX
Hardware managed cache
PPTX
Directory based cache coherence
PPTX
Cache recap
PPTX
Python your new best friend
PPTX
Python language data types
PPTX
Python basics
PPTX
Programming for engineers in python
PPTX
Learning python
PPTX
Extending burp with python
PPTX
Cobol, lisp, and python
PPT
Object oriented programming using c++
PPTX
Object oriented analysis
PPTX
Object model
PPTX
Data structures and algorithms
Rest api to integrate with your site
How to build a rest api
Api crash
Smm and caching
Optimizing shared caches in chip multiprocessors
How analysis services caching works
Hardware managed cache
Directory based cache coherence
Cache recap
Python your new best friend
Python language data types
Python basics
Programming for engineers in python
Learning python
Extending burp with python
Cobol, lisp, and python
Object oriented programming using c++
Object oriented analysis
Object model
Data structures and algorithms

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Machine Learning_overview_presentation.pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Unlocking AI with Model Context Protocol (MCP)
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Network Security Unit 5.pdf for BCA BBA.
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Machine Learning_overview_presentation.pptx
NewMind AI Weekly Chronicles - August'25-Week II
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
“AI and Expert System Decision Support & Business Intelligence Systems”
Spectroscopy.pptx food analysis technology
Programs and apps: productivity, graphics, security and other tools
gpt5_lecture_notes_comprehensive_20250812015547.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
sap open course for s4hana steps from ECC to s4
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Unlocking AI with Model Context Protocol (MCP)
The AUB Centre for AI in Media Proposal.docx
Building Integrated photovoltaic BIPV_UPV.pdf

Abstrac tinheritance 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/