Presentation is loading. Please wait.

Presentation is loading. Please wait.

Daniela Oliveira University of Florida

Similar presentations


Presentation on theme: "Daniela Oliveira University of Florida"— Presentation transcript:

1 Daniela Oliveira University of Florida
OOP: Public Interfaces, Static Members, Abstract Classes and Interfaces Daniela Oliveira University of Florida

2 Imperative/Procedural Programming Paradigm

3 Imperative/Procedural Programming Paradigm

4 Class A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common; a kind of category… The American Heritage Dictionary

5 Encapsulation

6 Implementation can change, Public Interface is the Same

7 Class Private Data (Variables) Public Interface (Methods) Private Methods

8 Public Interface of Cashier Register

9 Method Invocation, Objects and Instance Variables

10 Instance Variables and Objects

11 Who here is familiar with the concept of constructor
Who here is familiar with the concept of constructor? It is a special method used to initialize the object/

12 Static Variables SavingsAccount SavingsAccount SavingsAccount

13

14 Look how I am setting the interest rate without invoking any object!
And same thing when I print interest rate.

15 Note that the constructor is invoked every time an object is created
Note that the constructor is invoked every time an object is created. Then every time an object is created we increment numberOfAccounts and voila, we keep track of the number of objects.

16 A Generic Class Figure class Figure { double dim1, dim2;
Figure(double a, double b) { dim1 = a; dim2 = b; } double area() { System.out.println(“Sorry, not defined yet. Please do”) Now let’s discuss another important topic. Abstract classes. Let me start with an example. Consider a class Figure that stores dimensions of two-dimensional objects. (SLIDE) The class also defines a method area that computes the area of the fig based on the dimensions. The idea is that a programmer can create subclasses of figs of two dimensions by inheriting from the generic class Fig. Question: can you give me an example of two dimensional figs? A Triangle and a Rectangle. Is the algorithm ot formula to compute the areas of these two figs the same? No, they are rather different. The way Figure was defined implies that the developer needs to override the method area in each fig it creates.

17 A Generic Class Figure class Rectangle extends Figure{
Rectangle(double a, double b) { super(a, b); } double area() { return dim1 * dim2;

18 An Abstract Class Figure
double dim1, dim2; Figure(double a, double b) { dim1 = a; dim2 = b; } abstract double area(); Now let’s discuss another important topic. Abstract classes. Let me start with an example. Consider a class Figure that stores dimensions of two-dimensional objects. (SLIDE) The class also defines a method area that computes the area of the fig based on the dimensions. The idea is that a programmer can create subclasses of figs of two dimensions by inheriting from the generic class Fig. Question: can you give me an example of two dimensional figs? A Triangle and a Rectangle. Is the algorithm ot formula to compute the areas of these two figs the same? No, they are rather different. The way Figure was defined implies that the developer needs to override the method area in each fig it creates.

19 Interface Example Interface Animal { public void eat();
public void travel(); }

20 Implementing Animal

21 Extending an Interface

22 References Kim Bruce and America Chamber’s notes
Data Structures and Algorithms in Java. Robert Lafore. 2nd Edition SAMS 2003. Java - An Introduction to Problem Solving and Programming. 6th Edition Walter Savitch Prentice Hall – Pearson 2012


Download ppt "Daniela Oliveira University of Florida"

Similar presentations


Ads by Google