Download presentation
Presentation is loading. Please wait.
Published byWilliam Lyons Modified over 9 years ago
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 more difficult
2
Object Oriented Programming Emphasis on data Algorithms operations Features –Data abstraction –Encapsulation –Inheritance –Dynamic binding
3
OOPdefinitionJava Objectself contained entity encapsulating data and operations on that data Class object or class instance Instance variable maintains internal state of object Private data member Methodpublic 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
4
Relationships between classes Two classes are independent –Nothing in common Two classes are related by inheritance Two classes are related by composition
5
Inheritance hierarchy Animal Dogs toy poodle Sporting labrador retriever greyhound working German Shepherd CatHorse
6
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
7
Inheritance Examples Base ClassDerived Class StudentGraduateStudent UndergraduateStudent ShapeCircle Triangle Rectangle LoanCarLoan HomeImprovementLoan MortgageLoan EmployeeFaculty Staff AccountCheckingAccount SavingsAccount
8
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
9
modifier(s) class ClassName extends BaseCassName modifier(s) { memberlist } public class Circle extends Shape {. }
10
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
11
Java Programming: Program Design Including Data Structures 11 UML Class Diagram: class Rectangle
12
Java Programming: Program Design Including Data Structures12 UML Class Diagram: class Box
13
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; }
14
Java Programming: Program Design Including Data Structures14 class Box public 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; }
15
Java Programming: Program Design Including Data Structures15 class Box public double area() { return 2 * (getLength() * getWidth() + getLength() * height + getWidth() * height); } public double volume() { return super.area()* height); }
16
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());
17
Java Programming: Program Design Including Data Structures17 Objects myRectangle and myBox Rectangle myRectangle = new Rectangle(5, 3); Box myBox = new Box(6, 5, 4);
18
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
19
Method overloading public int square( int x ) { return x * x; } public double square( double y ) { return y * y; }
20
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.
21
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.
22
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.
23
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.