Download presentation
Presentation is loading. Please wait.
Published byPhilomena Baker Modified over 9 years ago
1
CSE 1201 Object Oriented Programming Introduction
2
Acknowledgement b For preparing the slides I took materials from the following sources Course Slides of Dr. Tagrul Dayar, Bilkent UniversityCourse Slides of Dr. Tagrul Dayar, Bilkent University Java book “Java Software Solutions” by Lewis & Loftus.Java book “Java Software Solutions” by Lewis & Loftus. 2
3
Outline b Course Objectives b Text Book b Objects b Classes b Abstractions b Encapsulations 3
4
Course Objectives b b Learning object-oriented programming with Java. b b Writing and enhancing Classes Arrays Inheritance and polymorphism Abstract classes and interfaces Graphical user interface I/O streams Exceptions. b b Practice communication in written and oral form 4
5
Text Book b Lewis & Loftus, “Java Software Solutions – Foundations of program design”, Addison Wesly, 8 th edition, 2014. b Deitel & Deitel, “ Java: How to program” 5
6
Java is an Object-Oriented Language b In structured programming languages, methods define the structure of the programs, they are basic building blocks b Data has secondary role, it is just something that is passed around. b In object oriented languages, the data has the principal role b Methods belong to the data, without the data, the method does not have any meaning (Except static methods) b Data and methods together make up the object. b OOP tries to model the real world. b What does the real world look like? 6
7
Objects everywhere... Real world entities 7
8
Objects have state... Red Lying Happy Hooked ill Broken 8
9
Objects have behavior…. Hello, I am John Nice to meet you da da … Grrrrrrrr Vroemm 9
10
World b The world is a set of thingsa set of things interacting with each other.interacting with each other. b OOP is more natural to humans, but less natural to computers b Computers (usually) have a single thread of control, so objects take turns 10
11
Describing the world b Describe a particular person Suraya has long blond hair, green eyes, is 1.63m tall, weighs 56Kg and studies computer engineering. Now sitting in the class room.Suraya has long blond hair, green eyes, is 1.63m tall, weighs 56Kg and studies computer engineering. Now sitting in the class room. Tanvir has short black hair and brown eyes. He is 180cm and 75 kilos. Now thinking to take a nap!Tanvir has short black hair and brown eyes. He is 180cm and 75 kilos. Now thinking to take a nap! b Notice how all have specific values of name, height, weight, eye colour, state, …name, height, weight, eye colour, state, … 11
12
Object Properties b Identity b State b Behavior myLamp on off Object is an abstraction of a real world entity 12
13
Introduction to Objects b An object represents something with which we can interact in a program b An object provides a collection of services that we can tell it to perform for us b The services are defined by methods in a class that defines the object b A class represents a concept, and an object represents the embodiment of a class b A class can be used to create multiple objects 13
14
Objects and Classes Bank Account A class (the concept) Alice’s Bank Account Balance: $5,257 An object (the realization) Pinky’s Bank Account Balance: $1,245,069 Sakib’s Bank Account Balance: $16,833 Multiple objects from the same class 14
15
Java OOP terminology b Class - Category Properties/statesProperties/states Functionality/Services (examines/alters state)Functionality/Services (examines/alters state) data methods object - Individual/unique thing (an instance of a class) Particular value for each property/state & functionality of all members of class. 15
16
Java OOP Software b Software System Set of objectsSet of objects Which interact with each otherWhich interact with each other One object will send a message to another object asking it to do a particular task. The first object does not need to know how the task is done (only how to request that it be done.) This corresponds to calling one of the second object’s methods! Created (instantiated) from class definitions Person TonmoyShahid “Shahid” Tonmoy: Say your name 16
17
17 Abstraction b An abstraction hides (or ignores) unnecessary details b denotes the essential properties of an object b One of the fundamental ways in which we handle complexity b Objects are abstractions of real world entities b Programming goal: choose the right abstractions Abstraction A car consists of four wheels an engine, accumulator and brakes.
18
Multiple Abstractions A single thing can have multiple abstractions Example: a protein is… b a sequence of amino acids b a complicated 3D shape (a fold) 18
19
Choosing Abstractions Abstractions can be about b tangible things (a vehicle, a car, a map) or b intangible things (a meeting, a route, a schedule) An example: An example: b Abstraction name: light b Light’s wattage (i.e.,energy usage) b Light can be on or off b There are other possible properties (shape, color, socket size, etc.), but we have decided those are less essential b The essential properties are determined by the problem 19
20
Object-Oriented Model methods data Object boundary 20
21
Example: Pencil locationdirection penDown homeupdownwrite 21
22
Encapsulation b the data belonging to an object is hidden, so variables are private b methods are public b we use the public methods to change or access the private data locationdirection penDown homeupdownwrite publicprivate 22
23
Programming Implications b Encapsulation makes programming easier As long as the contract is the same, the client doesn’t care about the implementationAs long as the contract is the same, the client doesn’t care about the implementation b In Java, as long as the method signatures are the same, the implementation details can be changed In other words, I can write my program using simple implementations; then, if necessary, I can replace some of the simple implementations with efficient implementationsIn other words, I can write my program using simple implementations; then, if necessary, I can replace some of the simple implementations with efficient implementations 23
24
Car Objects 24
25
Defining class Car b What are the common attributes of cars? b What are the common behaviors of cars? 25
26
Class Car Car color speed power drive turn right turn left stop attributes operations class name 26
27
in Java Car String color int speed int power drive() turnRight() turnLeft() stop() attributes or instance variables methods class name 27
28
Java Syntax public class Car { // attribute declarations private String color; private int speed; private int power; // method declarations public void drive() { // …. } public void turnRight() { // …. } public void turnLeft() { // …. } public void stop() { // …. }} Car String color int speed int power drive() turnRight() turnLeft() stop() 28
29
Class Pencil Pencil int location String direction home() up() down() write() attributes methods Name 29
30
Declaring objects b A class can be used to create objects b Objects are the instances of that class Car String color int speed int power drive() turnRight() turnLeft() stop() new 30
31
Java's "Building Blocks" b Data types primitive constructs (e.g., integers, floating point numbers, characters)primitive constructs (e.g., integers, floating point numbers, characters) b Class A description of a set of objectsA description of a set of objects used to create objectsused to create objects 31
32
Primitive Data b There are exactly eight primitive data types in Java b Four of them represent integers: byte, short, int, longbyte, short, int, long b Two of them represent floating point numbers: float, doublefloat, double b One of them represents characters: charchar b And one of them represents boolean values: booleanboolean 32
33
Declaring object variables b A class name can be used as a type to declare an object reference variable Person Abdullah; b An object reference variable holds the address of an object 33
34
Declaring Objects Person String name String birthDate int age getName() getAge …. Class Person Abdullah; Abdullah is of Class 34
35
Creating Objects We use the new operator to create an object Abdullah = new Person(); b Creating an object is called instantiation b An object is an instance of a particular class b We can combine declaration and creation: Person String name String birthDate int age getName() getAge …. Abdullah Class Object instance of refers to Person Abdullah = new Person(); is of Class 35
36
Declaring and Creating Objects Flower int age int length int weight getAge() getLength() …. Class karanfil = new Flower(); Object instance of refers to Flower karanfil; karanfil is of Class 36
37
Basic approach b Define class b Declare objects b Create objects b Use objects 37
38
Using objects The way you work with objects is to send them messagesThe way you work with objects is to send them messages Most statements using objects have the following structureMost statements using objects have the following structure object.method object.method –for example: thisPerson.setAge(24); This meansThis means –the object whose name is thisPerson –is sent the message setAge() –along with the "value" 24 The effect of this is to set the person's age to be 24 years oldThe effect of this is to set the person's age to be 24 years old 38
39
Example Person String name String birthDate int age setName(String name) setAge(int age) getName() …. Sakib Class Object instance of refers to is of Class Sakib = new Person(); Person Sakib; Sakib.setName( “Sakib Al Hasan“) ; Sakib Al Hasan Sakib.setAge(12); 12 39
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.