Part 1

OOP Concepts

    

Class Objects Inheritance Polymorphism Encapsulation Message passing

 The Benefits of Classes – Reusability -.What Are Classes?  Definition: – A class is a blueprint or prototype » Defines the variables and methods common to all objects of a certain kind.Software programmers use the same class. . and the same code to create many objects.

out.println("Hello world"). } } Literal or value Anything under double quotes .Anatomy of a class Capital letter class Hello { keyword identifier String is a class public static void main(String args []) { System.

 Class defines the state and behavior of objects.  A class is a user defined data type.  These are known as instances of a class  .In a Java program everything is encapsulated in a class. we can create “variables” of that type.  Once class type has been defined.

] [ methods declaration. ] }  .The basic form of class definition is: class classname [extends superclassname] { [ variable declaration.

 Example: class Rectangle { int length.Adding variables Instance variables are created whenever an object of the class is instantiated. int width. }  .

 Therefore add methods that are necessary for manipulating data contained in the class.  .Adding methods A class without methods that operate on data has no “life”.  The objects created by such a class cannot respond to any messages.

The general form of method declaration is: type methodname (parameterlist) { method-body. }  .

int y) { length = x. int width.Example: class Rectangle { int length. } } . void getData (int x. width = y.

class Rectangle { int length. int y) void getData (int x. { length = x. width = y.Example: Assume we want to compute the area of the rectangle. width. } .

return (area). } } .int rectArea( ) { int area = length * width.

Objects  An object is a thing. Example of Objects John Mary Custome r Dog 238-49-1357 Account Object name Object „type‟ .

.  A common visual representation of a software object.What Is an Object? These real-world objects all have states and behaviors.  Definition: An object is a software bundle of variables and related methods (function).

. The object reference is assigned to the object reference variable.Creating Objects objectReference = new ClassName(). Example: Circle c= new Circle().

 Objects in Java are created using the new operator.  The new operator creates an object of the specified class and returns a reference to the object.  .Creating an object is also referred as instantiating an object.

Advantage of objects Modularity:  Information-hiding:  Code re-use:  Plug-ability and debugging ease:  .

Everything is an object.A program is a bunch of objects telling each other what to do by sending messages. 2. How does objects interact  Methods form the object's interface For example TV remote is an interface between you and the TV  .1.

Accessing class members The general form: objectname. variable name objectname. . methodname (parameter-list).

– States: » Gear & Speed – Behaviors: » Brake & Accelerate  Subclasses can – Add variables and methods.  Benefits: – Provide specialized behaviors – Reuse the code in the superclass – Abstract classes -. – Override inherited methods.What Is Inheritance? (extends)  Subclasses inherit variables and methods from superclasses. .define "generic" behaviors.

out.println(“Arhh").} } One Java file can only have one public class Mankind is-a Animal .Mankind extends Animal public class Animal { private int height = 0.println("Hello"). private int weight = 0. public void talk( ) { System.out. public void talk( ) { System.} } Should be in different files public class Mankind extends Animal { private int iq = 120.

 classes to inherit commonly used state and behavior from other classes  Any number of sub classes .

“Morphism”=forms Bird Move() Goose Move() Penguin Move() .Polymorphism  “Poly”=Many.

 It plays an important role in allowing objects having different internal structures to share the same external interface  .Polymorphism means the ability to take more than one form.

Encapsulation  Encapsulation is to hide the variables or something inside a class. preventing unauthorized parties to use. Account Balance Withdraw Deposit Transfer .

 When object A wants object B to perform one of B's methods.  Software objects interact and communicate with each other by sending messages to each other.What Are Messages? Message in an object. . object A sends a message to object B.

The name of the method to perform (changeGears). yourBicycle. The object to whom the message is addressed (Your Bicycle). 2.Messaging  Three components comprise a message: 1.changeGears(lowerGear) . 3. Any parameters needed by the method (lowerGear).

. – Messages can be sent and received between processes in the same machine or in different machines. Benefits: – Message passing supports all interactions between objects.