You are on page 1of 28

Part 1

OOP Concepts

Class Objects Inheritance Polymorphism Encapsulation Message passing

What Are Classes?

Definition:
A class is a blueprint or prototype
Defines the variables and methods common to all objects of a certain kind.

The Benefits of Classes


Reusability -- Software programmers use the same class, and the same code to create many objects.

Anatomy of a class
Capital letter

class Hello { keyword

identifier
String is a class

public static void main(String args []) { System.out.println("Hello world"); } } Literal or value
Anything under double quotes

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

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

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

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. Therefore add methods that are necessary for manipulating data contained in the class.

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

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

Example: Assume we want to compute the area of the rectangle.


class Rectangle { int length, width; int y)

void getData (int x,


{

length = x; width = y;
}

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

Objects

An object is a thing.

Example of Objects John Mary


Custome r Dog

238-49-1357
Account

Object name Object type

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).
A common visual representation of a software object.

Creating Objects
objectReference = new ClassName();

Example:
Circle c= new Circle();

The object reference is assigned to the object reference variable.

Creating an object is also referred as instantiating an object. 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.

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

1. Everything is an object.
2.A program is a bunch of objects telling each other what to do by sending messages. How does objects interact

Methods form the object's interface For example TV remote is an interface between you and the TV

Accessing class members


The general form:

objectname. variable name objectname. methodname (parameter-list);

What Is Inheritance? (extends)

Subclasses inherit variables and methods from superclasses.


States:
Gear & Speed

Behaviors:
Brake & Accelerate

Subclasses can
Add variables and methods. Override inherited methods.

Benefits:
Provide specialized behaviors Reuse the code in the superclass Abstract classes -- define "generic" behaviors.

Mankind extends Animal


public class Animal { private int height = 0; private int weight = 0; public void talk( ) { System.out.println(Arhh");} }

Should be in different files

public class Mankind extends Animal { private int iq = 120; public void talk( ) { System.out.println("Hello");} }

One Java file can only have one public class


Mankind is-a Animal

classes to inherit commonly used state and behavior from other classes

Any number of sub classes

Polymorphism

Poly=Many, Morphism=forms

Bird Move() Goose Move() Penguin Move()

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

Encapsulation

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

Transfer

What Are Messages?


Message in an object. Software objects interact and communicate with each other by sending messages to each other.

When object A wants object B to perform one of B's methods, object A sends a message to object B.

Messaging

Three components comprise a message:


1. The object to whom the message is addressed (Your Bicycle). 2. The name of the method to perform (changeGears). 3. Any parameters needed by the method (lowerGear).
yourBicycle.changeGears(lowerGear)

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

You might also like