Object Oriented Paradigm Programming Paradigms En Mohd Norafizal A.Aziz.
-
date post
22-Dec-2015 -
Category
Documents
-
view
223 -
download
3
Transcript of Object Oriented Paradigm Programming Paradigms En Mohd Norafizal A.Aziz.
Object Oriented ParadigmObject Oriented Paradigm
Programming ParadigmsProgramming Paradigms
En Mohd Norafizal A.AzizEn Mohd Norafizal A.Aziz
ContentsContents
Definition of object orientedDefinition of object oriented Basic concepts Basic concepts
ObjectsObjects ClassesClasses MessagesMessages EncapsulationEncapsulation InheritancesInheritances Abstract classes and InterfacesAbstract classes and Interfaces PolymorphismPolymorphism
Sample of code Sample of code ConclusionConclusion
Blind man and ElephantBlind man and Elephant
What is Object OrientedWhat is Object Oriented
Allows programmers to write computer Allows programmers to write computer programs by representing elements of a programs by representing elements of a real-world problem in the form of so-called real-world problem in the form of so-called objectsobjects..
Objects are represent both: Objects are represent both: behaviorsbehaviors of of real world objects as well as their real world objects as well as their characteristicscharacteristics. .
All work in this concepts are using All work in this concepts are using messagesmessages..
Advantages of Object OrientedAdvantages of Object Oriented
OOP provides a clear modular structure OOP provides a clear modular structure for programs.for programs.
OOP is easy to maintain. OOP is easy to maintain. OOP provides a good framework for code OOP provides a good framework for code
libraries. libraries.
ObjectsObjects
ObjectsObjects are the physical and conceptual things are the physical and conceptual things we find in the universe around us. we find in the universe around us.
Hardware, software, documents, human beings, Hardware, software, documents, human beings, and even concepts are all examples of objects. and even concepts are all examples of objects.
Objects are thought of as having state. The Objects are thought of as having state. The statestate of an object is the condition of the object, of an object is the condition of the object, or a set of circumstances describing the object. or a set of circumstances describing the object.
However, it is possible for some objects to However, it is possible for some objects to change their own state. If an object is capable of change their own state. If an object is capable of spontaneously changing its own state, we refer spontaneously changing its own state, we refer to it as an “to it as an “object with lifeobject with life”. ”.
Objects is …Objects is …
An objects has a An objects has a statestate, exhibits , exhibits some well defined some well defined behaviorbehavior and and
has a has a unique identityunique identity..
Generally, OOP approach may be seen as Generally, OOP approach may be seen as manipulation with multiple objects. manipulation with multiple objects.
Programmers can : Programmers can : Create new objectsCreate new objectsSend message to existing objects Send message to existing objects Collect responses in a form of objects to Collect responses in a form of objects to
which other messages may be sent. which other messages may be sent.
Sample of codingSample of coding
BankAccount myAccount = new BankAccount ();BankAccount myAccount = new BankAccount ();
String myname = new String (‘Fizal’);String myname = new String (‘Fizal’);
Employee meAsemployee = new Employee Employee meAsemployee = new Employee (myname, myAccount); (myname, myAccount);
Consists of three fundamentals part :
Variable declaration – Instantiation -- Initialization
ClassesClassesA class represent a set of objects that A class represent a set of objects that
share common structure and a common share common structure and a common behavior. behavior.
Example: CAR Example: CAR State ( gear, speed, etc)State ( gear, speed, etc)Behavior (change gear, accelerate etc)Behavior (change gear, accelerate etc)
Classes is …Classes is …
Is a prototype that defines Is a prototype that defines variablesvariables and and methods methods that are that are
common to all objects of the same common to all objects of the same kind.kind.
Sample of codingSample of coding
Public class EmployeePublic class Employee
{{
// // variablesvariables
private String Name; private String Name;
private BankAccount B_Account;private BankAccount B_Account;
// // methodsmethods
public String salary (int sum)public String salary (int sum)
{{
// method body// method body
}}
}}
Public class NamePublic class Name
{{
private String nama; private String nama;
public Name () // default constructorpublic Name () // default constructor
{}{}
public Name (String nama)public Name (String nama)
{{ this.nama = nama; }this.nama = nama; }
String getNama()String getNama()
{{ return nama; }return nama; }
void setNama(String Nama)void setNama(String Nama)
{{ this.nama = nama;this.nama = nama; }}
public String toString()public String toString()
{{ return nama return nama }}
}}
class Bicycleclass Bicycle
{ int cadence = 0; { int cadence = 0;
int speed = 0; int speed = 0;
int gear = 1; int gear = 1;
void changeCadence(int newValue) void changeCadence(int newValue)
{ cadence = newValue; } { cadence = newValue; }
void changeGear(int newValue) void changeGear(int newValue)
{ gear = newValue; } { gear = newValue; }
void speedUp(int increment) void speedUp(int increment)
{ speed = speed + increment; } { speed = speed + increment; }
void applyBrakes(int decrement) void applyBrakes(int decrement)
{ speed = speed - decrement; } { speed = speed - decrement; }
void printStates() void printStates()
{ System.out.println("cadence:"+cadence+" { System.out.println("cadence:"+cadence+" speed:"+speed+" gear:"+gear); } speed:"+speed+" gear:"+gear); }
} }
Import java.io; Import java.io;
Public class helloPublic class hello
{{
public static void main (String args [])public static void main (String args [])
{{
System.out.pritnln(“Hello…World”);System.out.pritnln(“Hello…World”);
}}
}}
MessagesMessages
Single objects are not very useful. Instead, Single objects are not very useful. Instead, an object usually appears as just one an object usually appears as just one object of an object-oriented program that object of an object-oriented program that contains many objects. contains many objects.
In such program all works is done by In such program all works is done by interaction between these objects. interaction between these objects.
Sample of messagesSample of messages
Employee meAsemployee = new Employee(myname, Employee meAsemployee = new Employee(myname, myAccount); myAccount);
meAsemployee.salary(1000);meAsemployee.salary(1000);
ReferencesReferences
Java Tutorials: Java Tutorials: http://java.sun.com/docs/books/tutorial/java/concepts/http://java.sun.com/docs/books/tutorial/java/concepts/
Wikipedia:Wikipedia:
http://en.wikipedia.org/wiki/Object-oriented_programminghttp://en.wikipedia.org/wiki/Object-oriented_programming
Keywords: Keywords: object oriented concepts in java with examples.object oriented concepts in java with examples. Object Oriented conceptsObject Oriented concepts