Introduction To Object Oriented Programming: Week 2
Introduction To Object Oriented Programming: Week 2
Lecture Outlines
Object, Class, Message, Method & Attributes Object-Oriented Programming Characteristics
Learning Objectives
To describe fundamental concepts of OOP. To state the difference between objects
and classes To identify characteristics of OOP. To draw a basic of UML class diagram.
Introduction to OOP
Two popular programming design methods: Structured Programming (SP) Object-Oriented Programming (OOP)
Structured Programming(SP)
Other name: top-down, stepwise, modular Main problem will be divided into subproblem Analyze each sub-problem All sub-problem solutions will be combined to solves the main problem
Designs a program as collections of interacting objects that work together to accomplish tasks. Model the world as it is. The main concepts OO:
Objects, Classes and Instances Attributes and operations (behaviour) Object State Message-passing and methods
Objects
An Object is a thing, both tangible and intangible. Account, Vehicle, Employee, etc. Objects have state, behaviour and identity.
State: the condition of an object at any moment, affecting how it can behave Behaviour: what an object can do, how it can respond to events and stimuli Identity: each object is unique
Objects are categorized into classes Each individual object is an instance of a class
Class
To create an object inside the computer program, we must provide a definition for objectshow they behave and what kinds of information they maintain called a Class. Class - a category of objects that share the same: attributes, operations/methods Attribute keeping objects characteristics Operation represents objects behavior.
Attribute
Attribute- a named property of a class that describes a range of values that instances of the attribute might hold Attributes are the way classes encapsulate data Example: Class : Employee Attributes: employee_name, employee_id, position, department etc..
Operation
A behavior of an object Implemented in classes are methods Methods are identified and invoked by their signatures, including name, parameters, and return type Example:
Class : Employee Attributes: employee_name, employee_id, position, department, salary etc.. Operations: setEmployeeDetails calculateSalary printEmployeeDetails
Summary
Class Template (blue print) to define specific instances or objects Object Instantiation of a class Attributes Describes the object Behaviors/Operation specify what object can do Methods implement an objects behavior
Analogous to a function or procedure
Class Declaration
Basic skeleton of Java class
/* Sample program: Displaying Welcome */ public class Greeting { public static void main (String[] args) { System.out.println(Welcome); } }
Java Anatomy
Class title Comments Reserved words Identifiers Statements Blocks Classes Methods The main method Class name
//printing welcome public class Greeting { public static void main(String[] args) { System.out.println(Welcome!"); block } } block Main method signature String
Class Declaration
The Java class declaration takes the following form:
<modifier>* class <class_name> { <attribute_declaration>* <method_declaration>* }
Class Declaration
public class Cd { class private String title; attributes private String no_of_stock; public void setData(){ title = Animal Planet; no_of_stock = 45; Method definition } public void print(){ System.out.println(This CD title is + title + and still available + no_of_stock + copies); } }
Sending Message..
print()
myjCd : Cd
Creating Object
In Java, instance/object is created by using new keyword. Basic syntax: <class name> <object name> = new <class name> (parameters); Example:
Cd myCd = new Cd ();
Example:
myCd This is an object named myCd.
}
Accessing print() method (sending message print to object myCd)
Characteristics of OOP
Abstraction Encapsulation Inheritance Polymorphism
Abstraction..
Abstraction is the process of finding the essential feature set for a class. Concentrating on what an object is and does before making any decision about how the object will be implemented. Capture the attributes and operation of a object/real-world deck. Object in a program is an abstraction of the real-world object. Example: myCd.
Encapsulation..
Encapsulation is the process of hiding object data and providing methods for data access (data and behavior are bundled into a class and hidden from the outside world) Process of hiding the implementation details. Each object of a class has its own set of instance fields. Instance fields are declared with the access modifier private, public or protected. Access to the data and behavior is provided and controlled through an objects interface Useful, because programmer can reuse the program code. Data, element, and method is being encapsulated in a class, and can be use by the other class.
Inheritance..
Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class, or superclass, or base class The derived class is called the child class or subclass As the name implies, the child inherits characteristics of the parent A subclass inherits accessible data fields and methods from its super class, and may also add new data fields and methods.
Inheritance..
Super class (more general)
Inheritance..
Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class
Vehicle
Car
Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parent
Inheritance..
Class A
Class B
Class C
Class D
Class E
Inheritance..
Polymorphism..
The ability for different classes of objects to respond to identical messages in different ways
Polymorphism = having many forms Different behaviors for the same message
Polymorphism..
Can be overloading or overriding. Overloading methods appear in the same class have the same name but, have different parameter lists, and, can have different return types
Overriding methods overriding allows a subclass to re-define a method it inherits from it's superclass overriding methods: appear in subclasses have the same name as a superclass method have the same parameter list as a superclass method have the same return type as as a superclass method