Upload
brandy-jones
View
36
Download
0
Embed Size (px)
DESCRIPTION
concept about oops methodology
Citation preview
OOP ConceptsOOP Concepts
Part 1Part 1
Class Class ObjectsObjects InheritanceInheritance PolymorphismPolymorphism EncapsulationEncapsulation Message passingMessage passing
What Are What Are ClassesClasses??
Definition:Definition:– A class is a blueprint or prototypeA class is a blueprint or prototype
» Defines the variables and methods common to all objects of a Defines the variables and methods common to all objects of a certain kind.certain kind.
The Benefits of ClassesThe Benefits of Classes– Reusability -- Software programmers use the Reusability -- Software programmers use the
same class, and the same code to create many same class, and the same code to create many objects.objects.
Anatomy of a classAnatomy of a class
class Helloclass Hello
{{
public static void main(String args [])public static void main(String args [])
{{
System.out.println("Hello world");System.out.println("Hello world");
}}
}}
Capital letter
String is a classkeyword
identifier
Literal or valueAnything under double quotes
In a Java program everything is In a Java program everything is encapsulated in a class.encapsulated in a class.
A class is a user defined data type.A class is a user defined data type. Class defines the state and behavior of Class defines the state and behavior of
objects.objects. Once class type has been defined, we can Once class type has been defined, we can
create “variables” of that type.create “variables” of that type. These are known as instances of a classThese are known as instances of a class
The basic form of class definition is:The basic form of class definition is:
class classname [extends class classname [extends superclassname]superclassname]
{{
[ variable declaration; ][ variable declaration; ]
[ methods declaration; ][ methods declaration; ]
}}
Adding variablesAdding variables
Instance variables are created whenever an Instance variables are created whenever an object of the class is instantiated.object of the class is instantiated.
Example:Example:class Rectangleclass Rectangle{{
int length;int length;int width;int width;
}}
Adding methodsAdding methods
A class without methods that operate on A class without methods that operate on data has no “life”.data has no “life”.
The objects created by such a class cannot The objects created by such a class cannot respond to any messages.respond to any messages.
Therefore add methods that are necessary Therefore add methods that are necessary for manipulating data contained in the class.for manipulating data contained in the class.
The general form of method declaration is:The general form of method declaration is:
type methodname (parameter-type methodname (parameter-list)list)
{{
method-body;method-body;
}}
Example:Example:class Rectangleclass Rectangle
{{
int length;int length;
int width;int width;
void getData (int x, int y)void getData (int x, int y)
{{
length = x;length = x;
width = y;width = y;
}}
}}
Example:Example:Assume we want to compute the area of Assume we want to compute the area of the rectangle.the rectangle.class Rectangleclass Rectangle{{
int length, width;int length, width;
void getData (int x, int void getData (int x, int y)y)
{{length = x;length = x;width = y;width = y;
}}
int rectArea( )int rectArea( ){{
int area = length * int area = length * width;width;
return (area);return (area);}}
}}
ObjectsObjects
An An objectobject is a thing. is a thing.
Example of Example of ObjectsObjects
CustomerCustomer
John
DogDog
Mary
AccountAccount
238-49-1357
Object ‘type’
Object name
What Is an What Is an ObjectObject??
These real-world objects all have These real-world objects all have statesstates and and behaviorsbehaviors..
Definition: An object is a software bundle of Definition: An object is a software bundle of variablesvariables and related and related methods methods ((functionfunction).).
A common visual representation of a software object.
Creating ObjectsCreating Objects
objectReference = new ClassName();objectReference = new ClassName();
Example:Example:Circle c= new Circle();Circle c= new Circle();
The object reference is assigned to the object The object reference is assigned to the object reference variable.reference variable.
Creating an object is also referred as Creating an object is also referred as instantiating an object.instantiating an object.
Objects in Java are created using the Objects in Java are created using the newnew operator.operator.
The new operator creates an object of the The new operator creates an object of the specified class and returns a reference to the specified class and returns a reference to the object.object.
Advantage of objectsAdvantage of objects Modularity: Modularity: Information-hiding:Information-hiding: Code re-use:Code re-use: Plug-ability and debugging ease:Plug-ability and debugging ease:
1.1. Everything is an object. Everything is an object.
2.A program is a bunch of objects telling each 2.A program is a bunch of objects telling each other what to do by sending messages.other what to do by sending messages.
How does objects interact How does objects interact
Methods form the object's Methods form the object's interfaceinterface
For example TV remote is an interface For example TV remote is an interface between you and the TVbetween you and the TV
Accessing class membersAccessing class members
The general form:The general form:
objectname. variable nameobjectname. variable name
objectname. methodname objectname. methodname (parameter-list);(parameter-list);
What Is What Is InheritanceInheritance? (? (extendsextends)) Subclasses Subclasses inheritinherit variablesvariables and and methodsmethods from superclasses. from superclasses.
– States:States:» Gear & SpeedGear & Speed
– Behaviors:Behaviors:» Brake & Accelerate Brake & Accelerate
Subclasses can Subclasses can – Add Add variablesvariables and and methodsmethods..– OverrideOverride inheritedinherited methodsmethods..
Benefits:Benefits:– Provide specialized behaviorsProvide specialized behaviors– Reuse the code in the superclassReuse the code in the superclass– Abstract classesAbstract classes -- define "generic" behaviors. -- define "generic" behaviors.
MankindMankind extendsextends Animal Animal
public public classclass AnimalAnimal { { private int height = 0;private int height = 0; private int weight = 0;private int weight = 0; public void talk( ) { System.out.println(“Arhh");}public void talk( ) { System.out.println(“Arhh");}}}
public public classclass MankindMankind extends Animalextends Animal { { private int iq = 120;private int iq = 120; public void talk( ) { System.out.println("Hello");}public void talk( ) { System.out.println("Hello");}}}
Mankind is-a Animal
Should be in different files
One Java file can only have one public class
classes to classes to inheritinherit commonly used state and commonly used state and behavior from other classesbehavior from other classes
Any number of sub classesAny number of sub classes
PolymorphismPolymorphism ““Poly”=Many, “Morphism”=formsPoly”=Many, “Morphism”=forms
BirdBird
Move()Move()
GooseGoose
Move()Move()PenguinPenguin
Move()Move()
Polymorphism means the ability to take Polymorphism means the ability to take more than one form.more than one form.
It plays an important role in allowing It plays an important role in allowing objects having different internal structures objects having different internal structures to share the same external interfaceto share the same external interface
EncapsulationEncapsulation Encapsulation is to hide the variables or something inside a class, preventing unauthorized parties to use.
Account
Withdraw
Deposit
Transfer
Balance
What Are Messages?What Are Messages?
Message in an object.Message in an object. Software objects interact and communicate Software objects interact and communicate
with each other by sending messages to with each other by sending messages to each other. each other.
When object A wants object B to perform one of B's methods, object A sends a message to object B.
MessagingMessaging
Three components comprise a message:Three components comprise a message:1.1. The object to whom the message is addressed The object to whom the message is addressed
(Your Bicycle).(Your Bicycle).2.2. The name of the method to perform The name of the method to perform
(changeGears).(changeGears).3.3. Any parameters needed by the method Any parameters needed by the method
(lowerGear).(lowerGear).yourBicycle.changeGears(lowerGear)
Benefits:Benefits:– Message passing supports all interactions Message passing supports all interactions
between objects. between objects. – Messages can be sent and received between Messages can be sent and received between
processes in the same machine or in different processes in the same machine or in different machines.machines.