structured programming good for programming in the small often doesn't scale up limitations...
TRANSCRIPT
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
Object Oriented Programming
• Emphasis on data
• Algorithms operations
• Features– Data abstraction– Encapsulation– Inheritance– Dynamic binding
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
Relationships between classes
• Two classes are independent– Nothing in common
• Two classes are related by inheritance
• Two classes are related by composition
Inheritance hierarchy
Animal
Dogs Cat Horse
toy Sporting working
poodle labrador retriever greyhound German Shepherd
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
Inheritance ExamplesBase Class Derived Class
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee Faculty
Staff
Account CheckingAccount
SavingsAccount
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
modifier(s) class ClassName extends BaseCassName modifier(s){ memberlist}
public class Circle extends Shape{ . . .}
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
Java Programming: Program Design Including Data Structures
11
UML Class Diagram: class Rectangle
Java Programming: Program Design Including Data Structures
12
UML Class Diagram: class Box
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;}
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;}
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);}
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());
Java Programming: Program Design Including Data Structures17
Objects myRectangle and myBox
Rectangle myRectangle = new Rectangle(5, 3);Box myBox = new Box(6, 5, 4);
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
Method overloading
public int square( int x )
{
return x * x;
}
public double square( double y )
{
return y * y;
}
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.
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.
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.
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)