Download - Abstrac tinheritance polymorphism
![Page 1: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/1.jpg)
Abstraction, Inheritance, and Polymorphism
in Java
![Page 2: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/2.jpg)
“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.”
![Page 3: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/3.jpg)
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
![Page 4: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/4.jpg)
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... :)
![Page 5: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/5.jpg)
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
![Page 6: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/6.jpg)
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)
![Page 7: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/7.jpg)
OOP
![Page 8: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/8.jpg)
Case Study: Dance Studio
![Page 9: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/9.jpg)
Quiz:
How many classes we wrotefor this applet?
A. 1B. 2C. 5D. 10E. 17
![Page 10: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/10.jpg)
DanceStudio
DanceModel
Music
DanceFloor
Controller
Model
View
MaleDancerFemaleDancer
MaleFootFemaleFoot
Dancer
Foot
![Page 11: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/11.jpg)
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
![Page 12: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/12.jpg)
In a project with 10 classes we need an IDE...
![Page 13: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/13.jpg)
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?
![Page 14: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/14.jpg)
Abstraction
MaleDancer FemaleDancer
Dancer
![Page 15: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/15.jpg)
EncapsulationEncapsulation 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.
![Page 16: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/16.jpg)
Encapsulation
MaleFoot FemaleFoot
Foot
![Page 17: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/17.jpg)
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
![Page 18: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/18.jpg)
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”)
![Page 19: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/19.jpg)
True or False? Abstraction and encapsulation are helpful for the following:
Team development ________
Reusable software ________
GUI programming ________
Easier program maintenance ________
![Page 20: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/20.jpg)
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
![Page 21: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/21.jpg)
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.
![Page 22: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/22.jpg)
Inheritance Terminology:
public class FemaleDancer extends Dancer{ ...}
subclass
or
derived class
superclass
or
base class
extends
![Page 23: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/23.jpg)
MaleDancer FemaleDancer
Dancer
Example:
Inheritance (cont’d)
![Page 24: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/24.jpg)
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:
![Page 25: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/25.jpg)
MaleFoot FemaleFoot
Foot
Example:
Inheritance (cont’d)
![Page 26: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/26.jpg)
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:
![Page 27: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/27.jpg)
Inheritance may be used to define a hierarchy of classes in an application:
MaleFoot FemaleFoot
Foot
MaleLeftFoot MaleRightFoot FemaleLeftFoot FemaleRightFoot
Object
![Page 28: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/28.jpg)
• 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
![Page 29: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/29.jpg)
True or False? Inheritance is helpful for the following:
Team development ________
Reusable software ________
GUI programming ________
Easier program maintenance ________
![Page 30: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/30.jpg)
Answer:
Team development ________
Reusable software ________
GUI programming ________
Easier program maintenance ________
F
T
???
T
![Page 31: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/31.jpg)
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.
![Page 32: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/32.jpg)
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.
![Page 33: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/33.jpg)
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:
![Page 34: Abstrac tinheritance polymorphism](https://reader035.vdokument.com/reader035/viewer/2022062822/5883fd101a28ab884b8b5055/html5/thumbnails/34.jpg)
These slides and the Dance Studio code are posted at: http://www.skylit.com/oop/