Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Object-Oriented Concepts

Similar presentations


Presentation on theme: "Introduction to Object-Oriented Concepts"— Presentation transcript:

1 Introduction to Object-Oriented Concepts

2 What is this class about?
Three Questions What is this class about? Why does it matter? How does 331 fit in cs curriculum?

3 Computational Problems
What is computer science? Computer Solution? ? Computational Problems What is this class about?

4 Solving Problems With The Aid of Computers
Communication? Programming language Machine language What is this class about?

5 Solving Problems With The Aid of Computers
Programs Programming language Machine language What is this class about?

6 Solving Problems With The Aid of Computers
Programs Programming language Machine language Address Machine Instruction   1   2   3   4   What is this class about?

7 Assembly Instructions
Solving Problems With The Aid of Computers Programs Programming language Machine language Assembly Instructions push cs pop ds mov ah, 9 int 21h sloop: What is this class about?

8 Solving Problems With The Aid of Computers
Programs Programming language Machine language High-level Program for(int i=0; i<10; i++) System.out.println(list[i]); What is this class about?

9 Procedural Programming
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include<stdio.h> // function prototype, also called function declaration float square ( float x );                               // main function, program starts from here int main( )               {     float m, n ;     printf ( "\nEnter some number for finding square \n");     scanf ( "%f", &m ) ;     // function call     n = square ( m ) ;                           printf ( "\nSquare of the given number %f is %f",m,n ); } float square ( float x )   // function definition     float p ;     p = x * x ;     return ( p ) ; What is this class about?

10 Structure of a Procedural Program
Procedural Programming Structure of a Procedural Program function-1() main() In procedural programming a program consists of a set of functions. The first function to be executed is often called ‘main’. What is this class about?

11 Challenges of Procedural Programming
Handling large complex programs. create reuse evolve Large software systems: 4 x 106 – 100 x 106 What is this class about?

12 Dimensions of Software Complexity
Challenges of Procedural Programming Dimensions of Software Complexity Algorithmic Functional Structural structure of the data structure of the program Communicational Computational etc. What is this class about?

13 Complex programs require systematic development
Challenges of Procedural Programming Complex programs require systematic development Requirements Analysis Software Design Implementation Testing Deployment Evolution What are the most difficult phases of the software process?

14 Complex programs require systematic design
Challenges of Procedural Programming Complex programs require systematic design Requirements Analysis Software Design Implementation Testing Deployment Evolution Software design is arguably the most difficult phase of the software lifecycle.

15 Programming Paradigm Evolution
Object-Oriented Programming, C++, Java Procedural Programming: C, Pascal, Fortran Assembly Language Programming Machine Language Programming OOP provides better tools for developing and managing complex programs. What is this class about?

16 Object-Oriented Programming using Java
What is this class about? Object-Oriented Programming using Java OOP provides better tools for developing and managing complex programs. What is this class about?

17 Why does it matter? Object-Oriented Programming (OOP) is a dominant programming paradigm Leverage benefits of Object-Oriented Programming OOP provides better tools for developing and managing complex programs. What is this class about?

18 What is this class about?
Three Questions What is this class about? Why does it matter? How does 331 fit into cs curriculum?

19 How does 331 fit into cs curriculum?
131 – basic procedural programming 231 – basic structures for managing data 331 – you learn basic OOP Operating systems (use OOP) 380 – algorithm design (use OOP) 450 – teams develop complex software (use OOP) 455 – includes OO databases

20 Outline Course overview Getting to know you/me
Introduction to Object Oriented Programming (OOP) Concepts Encapsulation Classes & Objects Composition Inheritance Polymorphism

21 Getting to Know You Name Background Where from Programming background
The Man without a Face, oil on panel, 11x14, 2005 – Dae-Woong Nam Name Background Where from Programming background Languages

22 Fundamental Concepts of OOP
Encapsulation Inheritance Polymorphism

23 Understanding Object Oriented Programming
In machine language programming – programs are sequences of bits. In assembly language programming – programs are pieces of code. In procedural programming – programs are collections of interacting functions. In OOP – programs are collections of interacting objects.

24 Program Structure: OOP vs Procedural
Object-Oriented Program global data function-1() main() Procedural Program function-1() main() global data Class 1 function-1() global data Class 2 function-1() global data Class 3

25 Requirements Analysis
OOP & Design OOP simplifies software design because OOP uses larger abstraction units than procedural programming. Requirements Analysis Software Design Implementation Testing Deployment Evolution

26 Motivating Example Bank Client StockBroker
In OOP objects in the real world are modeled as objects in software world. Bank StockBroker Client Programming languages represent real world objects as software classes and objects. OOP narrows the semantic gap between real world and software world.

27 Example of a Java Class public class Account { private int accNumber;
private double balance; public boolean deposit(double amount){ if(amount > 0){ this.balance += amount; return true; } return false; public boolean withdraw(double amount){ if(this.balance >= amount){ this.balance -= amount; }//end of class

28 Method Signatures The following is all the user needs to know to effectively use a method: The name of the method The parameters passed to the method The return type of the method What he method does

29 Example of a Java Class public class Account { private int accNumber;
private double balance; public boolean deposit(double amount){ if(amount > 0){ this.balance += amount; return true; } return false; public boolean withdraw(double amount){ if(this.balance >= amount){ this.balance -= amount; }//end of class

30 Abstraction & Software Design
In procedural programming: Functions are black boxes. all a user needs to know to effectively use a method is the method signature:

31 Abstraction & Software Design
In procedural programming: Functions are black boxes. all a user needs to know to effectively use a method is the method signature: In OOP: Classes and functions are black boxes. all a user needs to know to effectively use a class is the class interface: The class interface includes a list of method signatures The service the class provides

32 O-O Programming A fundamental advantage of O-O programming is that the data and the operations that manipulate the data are both contained in the object. For example, when an object is transported across a network, the entire object, including the data and behavior, goes with it.

33 Class Fundamentals & Modeling in UML
Account deposit(amount:double):boolean withdraw(amount:double):boolean accNumber:int balance:double In OOP, class = methods + data We call the data ‘attributes’ of the class

34 IN-Class Exercise Draw a UML diagram for a class that represents books in a library software system. Name Attributes – define data the class will use. Operations/methods/functions – define the class behavior.

35 Object Data The data stored within an object represents the state of the object. In O-O programming terminology, this data is called attributes. Eg. 1: UML account class with account number, balance. E.g. 2: Java account class with account number, balance.

36 Object Data in Java In Java data/variables must be declared before use. Java uses explicit typing. eg. Java account class with attributes: account number and balance. public class Account { private int accNumber; private double balance; } }//end of class

37 Fundamental Concepts of OOP
Continue … Fundamental Concepts of OOP Encapsulation Inheritance Polymorphism

38 Encapsulation One of the primary advantages of using objects is that the object need not reveal all its attributes and behaviors. In good O-O design (at least what is generally accepted as good), an object should only reveal the interfaces needed to interact with it.

39 Encapsulation Details not pertinent to the use of the object should be hidden from other objects. This is called encapsulation. Encapsulation is realized using classes and objects class = variables + methods

40 Encapsulation & Interfaces
The complete list of all the methods a user may call to get a desired behavior from an object is called the provided or public interface of the class/object. This interface also includes any publicly accessible attributes. Interfaces do not normally include attributes only methods. The interface should completely describe how users of the class interact with the class. methods that are part of the interface are designated as public.

41 Encapsulation & Interfaces
public class Account { private int accNumber; private double balance; Public String alias; public void deposit(double amount){ this.balance += amount; } public void withdraw(double amount){ this.balance -= amount; private int changeAccNumber(int accNum){ this.accNumber = accNum; private void getAccountInfo(){ //actual code here }//end of class What is the provided interface for this class?

42 Encapsulation & Interfaces
Normally, if a user needs access to an attribute, then a public method is created to return the attribute. To support OO design, OOP languages facilitates the creation of: Classes = attributes + methods Interfaces = list of method signatures

43 Encapsulation & Interfaces
Example of an interface public interface IAccount { public boolean deposit(double amount); public boolean withdraw(double amount); public int getAccNumber(); public double getBalance(); public void setBalance(double balance); }

44 Encapsulation: What Exactly Is a Class?
A class is a blueprint for an object. The process of creating an object from a class is called instantiation. When you instantiate an object, you use a class as the basis for how the object is built. NB: primitive types vs classes.

45 Encapsulation: Declaring variables in Java
Type of data is explicitly used: int x; float y, z; Integers and floats are built-in types Declare two instances of the Account class: Account obj1, obj2; Classes are user-defined types.

46 Fundamental Concepts of OOP
Encapsulation Inheritance Polymorphism

47 Inheritance Inheritance allows a class to inherit the attributes and methods of another class. This allows you to create brand new classes by abstracting out common attributes and behaviors.

48 Mammal Hierarchy Inheritance is sometimes described as the “Is A” relationship. For example, a dog is a mammal.

49 Mammal Hierarchy

50 Superclasses and Subclasses
The superclass, or parent class, contains all the attributes and behaviors that are common to classes that inherit from it.

51 Superclasses and Subclasses
A shape hierarchy The inheritance arrow shown here is called “Generalization/Sp ecialization” in UML

52 Inheritance Example in Java
Most common form is to use the extends keyword public class Dog extends Mammal{ //class body }

53 Fundamental Concepts of OOP
Encapsulation Inheritance Polymorphism

54 Polymorphism Polymorphism means many forms. Java provides two forms of polymorphism: Method overloading Operator overloading.

55 Polymorphism Method overloading
Multiple methods with the same name but having different method signatures. public void add(int a, int b); public void add(int a, int b, int c); public void add(String a, String b);

56 Polymorphism Operator overloading.
One operator may be used for different operations versus “I love ” + “you Mom.”

57 Composition It is natural to think of objects as containing other objects. A television set contains a tuner and video display.

58 Composition A computer contains video cards, keyboards, and drives.
Although the computer can be considered an object unto itself, the drive is also considered a valid object.

59 Composition In fact, you could open up the computer and remove the drive and hold it in your hand. Both the computer and the drive are considered objects. It is just that the computer contains other objects such as drives. In this way, objects are often built, or composed, from other objects: This is composition.

60 Has-a Relationships While an inheritance relationship is considered an Is-a relationship for reasons already discussed, a composition relationship is termed a Has-a relationship.

61 Has-a Relationships Using the example in the previous section, a television Has-a a tuner and Has-a video display. A television is obviously not a tuner, so there is no inheritance relationship.

62 Has-a Relationships In the same vein, a computer Has-a video card, Has-a keyboard, and Has-a disk drive. The topics of inheritance, composition and how they relate to each other is covered in great detail later in the course.

63 How Java Works See

64 Qu es ti ons? The End ______________________ Devon M. Simmonds
Computer Science Department University of North Carolina Wilmington _____________________________________________________________


Download ppt "Introduction to Object-Oriented Concepts"

Similar presentations


Ads by Google