structured programming good for programming in the small often doesn't scale up limitations...
TRANSCRIPT
![Page 1: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/1.jpg)
Structured Programming
• Good for programming in the small
• Often doesn't scale up
• Limitations– Changes at top may affect lower-level
algorithms– Code reuse more difficult
![Page 2: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/2.jpg)
Object Oriented Programming
• Emphasis on data
• Algorithms operations
• Features– Data abstraction– Encapsulation– Inheritance– Dynamic binding
![Page 3: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/3.jpg)
OOP definition Java
Object self contained entity encapsulating data and operations on that data
Class object or class instance
Instance variable
maintains internal state of object
Private data member
Method public operations, only means by which the object's state can be inspected or modified by another object
Public member function
Message passing
method of communicating between objects
Function call to public member function
![Page 4: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/4.jpg)
Relationships between classes
• Two classes are independent– Nothing in common
• Two classes are related by inheritance
• Two classes are related by composition
![Page 5: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/5.jpg)
Inheritance hierarchy
Animal
Dogs Cat Horse
toy Sporting working
poodle labrador retriever greyhound German Shepherd
![Page 6: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/6.jpg)
Inheritance
• Mechanism by which one class acquires the properties of another class
• Base class – class being inherited from• Derived class – the class that inherits• Is-a: every member of the derived class is also a
member of the base class– Derived class inherits all properties of base class
• allows us to create an object derived from another one, so that it may include some of the other's members plus its own
![Page 7: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/7.jpg)
Inheritance ExamplesBase Class Derived Class
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee Faculty
Staff
Account CheckingAccount
SavingsAccount
![Page 8: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/8.jpg)
Java Programming: Program Design Including Data Structures
8
Inheritance• “is-a” relationship• Single inheritance:
– Subclass is derived from one existing class (superclass)
• Multiple inheritance:– Subclass is derived from more than one
superclass– Not supported by Java– A class can only extend the definition of one
class
![Page 9: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/9.jpg)
modifier(s) class ClassName extends BaseCassName modifier(s){ memberlist}
public class Circle extends Shape{ . . .}
![Page 10: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/10.jpg)
superclasses and subclasses
• private members of superclass are not accessible to subclass
• Subclass can directly access public members of the superclass
• subclass can have more data or method members
• subclass class can redefine or override public methods of superclass
• All member variables and methods (unless overridden) of superclass are also members of the subclass
![Page 11: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/11.jpg)
Java Programming: Program Design Including Data Structures
11
UML Class Diagram: class Rectangle
![Page 12: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/12.jpg)
Java Programming: Program Design Including Data Structures
12
UML Class Diagram: class Box
![Page 13: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/13.jpg)
Java Programming: Program Design Including Data Structures13
Defining Constructors of the Subclass
• Call to constructor of superclass: – Must be first statement– Specified by super parameter list
public Box(){ super(); height = 0;}
public Box(double l, double w, double h){ super(l, w); height = h;}
![Page 14: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/14.jpg)
Java Programming: Program Design Including Data Structures14
class Boxpublic void setDimension(double l, double w, double h){ super.setDimension(l, w); if (h >= 0) height = h; else height = 0;}public double getHeight(){ return height;}
![Page 15: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/15.jpg)
Java Programming: Program Design Including Data Structures15
class Boxpublic double area(){ return 2 * (getLength() * getWidth() + getLength() * height + getWidth() * height);}public double volume(){ return super.area()* height);}
![Page 16: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/16.jpg)
public class SubClassSuperClassMethods{ public static void main(String[] args) { Rectangle myRectangle1 = new Rectangle(); //Line 1 Rectangle myRectangle2 = new Rectangle(8, 6); //Line 2
Box myBox1 = new Box(); //Line 3 Box myBox2 = new Box(10, 7, 3); //Line 4
System.out.println("Line 5: myRectangle1: " + myRectangle1); //Line 5
System.out.println("Line 6: Area of myRectangle1: "+ myRectangle1.area()); System.out.println("Line 7: myRectangle2: "+ myRectangle2); System.out.println("Line 8: Area of myRectangle2: "+ myRectangle2.area());
![Page 17: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/17.jpg)
Java Programming: Program Design Including Data Structures17
Objects myRectangle and myBox
Rectangle myRectangle = new Rectangle(5, 3);Box myBox = new Box(6, 5, 4);
![Page 18: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/18.jpg)
overloading
• Using the same name for operations on different types
• Method overloading allows several methods to share the same name but with a different set of parameters
• Each overloaded method performs a similar task
• Different parameters• Only considers parameters, not return type
![Page 19: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/19.jpg)
Method overloading
public int square( int x )
{
return x * x;
}
public double square( double y )
{
return y * y;
}
![Page 20: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/20.jpg)
Another common use of overloading is when defining constructors:
public Time( ) //default constructor
public Time( int hr, int min, int sec ) //general constructor
public Time( Time time ) //copy constructor
•Overloading occurs within a class.
![Page 21: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/21.jpg)
Overriding
• similar to overloading
• except that it occurs between classes and
• each method has the same signature.
• A subclass can override a superclass method by redefining that method
• When the method is mentioned by name in the subclass, then the subclass version is automatically used.
![Page 22: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/22.jpg)
overriding
Super class – Square, subclass – Cube
public double area( ) // Square class method
{
return getSide( ) * getSide( );
}
public double area( ) // Cube class overridden method
{
return super.area( ) * 6; // calls the Square version
}
Note: if super is omitted in the above call to area( ), then infinite recursion results.
![Page 23: Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse](https://reader031.vdocuments.mx/reader031/viewer/2022013101/56649f0d5503460f94c21df8/html5/thumbnails/23.jpg)
Java Programming: Program Design Including Data Structures23
• To write a method’s definition of a subclass, specify a call to the public method of the superclass– If subclass overrides public method of
superclass, specify call to public method of superclass:
super.MethodName(parameter list)– If subclass does not override public method of
superclass, specify call to public method of superclass:
MethodName(parameter list)