introduction to object oriented programming. object oriented programming technique used to develop...

54
Introduction to Object Oriented Programming

Upload: tiffany-french

Post on 28-Dec-2015

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Introductionto Object Oriented Programming

Page 2: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Object Oriented Programming

Technique used to develop programs revolving around the real world entities

In OOPs, every real life object has properties and behavior.

Page 3: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Features of OOP Language

Abstraction

Inheritance

Encapsulation

Polymorphism

Page 4: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Object

The basic entity of object oriented programming language.

Class itself does nothing but the real functionality is achieved through their objects.

Object is an instance of the class.

It takes the properties (variables) and uses the behavior (methods) defined in the class.

The concept that all objects, while being unique, are also part of a set of objects that have characteristics and behaviors in common

Page 5: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

State of an object

The set of values of the attributes of a particular object is called its state

Page 6: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Class

Defines the properties and behavior (variables and methods) that is shared by all its objects.

A class is a piece of computer program that serves as a template for the creation of an object

Page 7: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

NameAgeColorWeight

Name-LassieAge-4yColor-Brown/BlackWeight-60Kg

Name-TommyAge-6yColor-BlackWeight-65Kg

Name-sprinterAge-4yColor-Brown/BlackWeight-68Kg

Name-LanyAge-6yColor-Brown/BlackWeight-62Kg

Dog ClassObjects

Page 8: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Instance

The actual object created at run-time

The Lassie object is an instance of the Dog class

Page 9: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Abstraction

The process of abstraction in Java is used to hide certain details and only show the essential features of the object.

Page 10: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Encapsulation

Process of binding together the methods and data variables as a  single entity.

It keeps both the  data and functionality code safe  from the outside world.

It hides the data within the class and makes it available only through  the methods.

Java provides different accessibility scopes (public, protected, private ,default) to hide the data from outside.

Page 11: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Inheritance

Allows a class (subclass) to acquire the properties and behavior of another class (superclass).

In java, a class can inherit only one class (superclass) at a time but a class can have any number of subclasses.

It helps to reuse,

Page 12: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Polymorphism

Allows one interface to be used for a set of actions i.e. one name may refer to different functionality.

Polymorphism allows a object to accept different requests of a client and responds  according to the current state of the runtime system

Page 13: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Two types of polymorphism

Compile-time polymorphism

Runtime Polymorphism

Page 14: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Compile-time polymorphism

Method to be invoked is determined at the compile time.

Compile time polymorphism is supported through the method overloading concept

Method overloading means having multiple methods with same name but with different signature (number, type and order of parameters).

Page 15: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Runtime Polymorphism

Method to be invoked is determined at the run time.

The example of run time polymorphism is method overriding.

When a subclass contains a method with the same name and signature as in the super class then it is called as method overriding.

Page 16: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Dynamic binding

Page 17: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Declaring Classes

class MyClass { //field, constructor, and method declarations}The class body is the area between the braces.It contains constructors for initializing new objects, declarations for the fields that provide the state of the class and its objects, and methods to implement the behavior of the class and its objects.

Page 18: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Constructors

A class contains constructors that are invoked to create objects from the class blueprint

When you create a new instance (a new object) of a class using the new keyword, a constructor for that class is called

Constructors are used to initialize the instance variables (fields) of an object.

Page 19: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Constructors

Constructor name is class name. A constructors must have the same name as the class its in.

Default constructor is created automatically by the compiler only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created.

The default constructor initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans).

Page 20: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Differences between methods and constructors

There is no return type given in a constructor signature (header). The value is this object itself

There is no return statement in the body of the constructor

The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.

Page 21: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

this(...)

Calls another constructor in same class.

Page 22: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

super(...)

Use super to call a constructor in a parent class. Calling the constructor for the super class must be the first statement in the body of a constructor.

Page 23: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Passing Information to a Method or a Constructor

Parameters refers to the list of variables in a method declaration.

Arguments are the actual values that are passed in when the method is invoked.

When you invoke a method, the arguments used must match the declaration's parameters in type and order.

Page 24: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Declaring Member Variables

There are several kinds of variables

* Member variables in a class—these are called fields.

* Variables in a method or block of code—these are called local variables.

* Variables in method declarations—these are called parameters.

Page 25: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Field declarations are composed of three components

1. Zero or more modifiers, such as public or private.

2. The field's type.

3. The field's name.

Page 26: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Access Modifiers

control what other classes have access to a member field

* public modifier—the field is accessible from all classes.

* private modifier—the field is accessible only within its own class.

Page 27: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Types

Variables must have a type.

Use primitive types such as int, float, boolean, etc.

Or you can use reference types, such as strings, arrays, or objects.

Page 28: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Variable Types

Local variables . variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.

Instance variables . Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.

Class variables . Class variables are variables declared with in a class, outside any method, with the static keyword.

Page 29: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Transient Variables

member variables are part of the persistent state of the object

Member variables that are part of the persistent state of an object must be saved when the object is archived

transient keyword to indicate to the Java virtual machine that the indicated variable is not part of the persistent state of the object

Transient int totalAvg;

Page 30: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Volatile Variables

If a member variable is modified asynchronously by concurrently running threads

the volatile variable is loaded from memory before each use, and stored to memory after each use thereby ensuring that the value of the variable is consistent and coherent within each thread

Page 31: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Defining Methods

The required elements of a method declaration are the

Modifiers—such as public, private

method's return type

Name

a pair of parentheses () and parameter list in parenthesis

and a body between braces {}

Page 32: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Overloading Methods

distinguish between methods with different method signatures

means that methods within a class can have the same name if they have different parameter lists

Page 33: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Method Overriding

achieved when a subclass overrides non-static methods defined in the superclass.

The new method definition must have the same method signature (i.e., method name and parameters) and return type.

Only parameter types and return type are chosen as criteria for matching method signature

Page 34: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Creating Objects

A class provides the blueprint for objects

Create an object from a class

Creating Objects includes three parts

1. Declaration:

2. Instantiation: The new keyword is a Java operator that creates the object.

3. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

Page 35: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Declaring a Variable to Refer to an Object

To declare a variable.This declaration also reserves the proper amount of memory for the variable

type name;

declaring a variable to hold an object is just like declaring a variable to hold a value of primitive type

its value will be undetermined until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object. For that, you need to use the new operator,

Page 36: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Instantiating an Object

used new operator to create a new instance of an object.

The new operator instantiates a class by allocating memory for a new object of that type. new requires a single argument: a call to a constructor method.

Constructor methods are special methods provided by each Java class that are responsible for initializing new objects of that type.

Page 37: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Initializing an Object

classes provide constructor methods to initialize a new object of that type.

A class may provide multiple constructors to perform different kinds of initialization on new objects.

When looking at the implementation for a class, you can recognize the constructors because they have the same name as the class and have no return type.

Page 38: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Referencing an Object's Variables

To access an object's variables, simply append the variable name to an object reference with an intervening '.' (period).

dogObj1.name=“Lassie”;

dogObj1.age=1;

Page 39: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Calling an Object's Methods

Calling an object's method is similar to getting an object's variable.

To call an object's method, simply append the method name to an object reference with an intervening '.' (period),

provide any arguments to the method within enclosing parentheses.

If the method does not require any arguments, just use empty parentheses.

dogObj1.setName(“Lassie”);

Page 40: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

The Garbage Collector

The Java runtime environment has a garbage collector that periodically frees the memory used by objects that are no longer needed.

The Java garbage collector is a mark-sweep garbage collector that scans Java's dynamic memory areas for objects, marking those that are referenced.

After all possible paths to objects are investigated, those objects that are not marked (that is, not referenced) are known to be garbage and are collected.

Page 41: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Finalization

Before an object gets garbage collected, the garbage collector gives the object an opportunity to clean up after itself through a call to the object's finalize method.

This process is known as finalization.

protected void finalize() throws Throwable

Page 42: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Public, Abstract, and Final Classes

The public modifier declares that the class can be used by objects outside the current package. By default a class can only be used by other classes in the same package in which it is declared.

The abstract modifier declares that your class is an abstract class. An abstract class may contain abstract methods (methods with no implementation). Abstract classes are intended to be subclassed and cannot be instantiated.

the final modifier you declare that your class is final; that is, that your class cannot be subclassed.

Page 43: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Abstract Classes

a class that you define an abstract concept and should not be instantiated.

for example, Mammal class in the real world.

Have you ever seen an instance of Mammal ?

What you see instead are instances of Dogs,Cats,Lions,Bats

abstract class Number {

}

Page 44: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Abstract Methods

methods with no implementation.

abstract void getName();

Page 45: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Final Classes

declare a class as final; that is, that class cannot be subclassed.

final class Dog {

}

Page 46: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Final Methods

protect class's methods from being overridden,

can use the final keyword in a method declaration to indicate to the compiler that the method cannot be overridden by subclasses

Page 47: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Declaring Constants

To create a constant member variable in Java use the keyword final in your variable declaration.

final double AVOGADRO = 6.023e23;

Page 48: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Inheritance

Page 49: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

InheritanceClasses can be derived from other classes, thereby

inheriting fields and methods from those classes.

A class that is derived from another class is called a sub class (also a derived class, extended class, or child class).

The class from which the subclass is derived is called a super class (also a base class or a parent class).

public class Dog extends Mammal

{

}

Page 50: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

InheritanceCan reuse the fields and methods of the

existing class without having to write them

A subclass inherits the members (fields, methods, and nested classes) from its superclass.

Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Page 51: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

* The inherited fields can be used directly, just like any other fields.

* You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).

* You can declare new fields in the subclass that are not in the superclass.

* The inherited methods can be used directly as they are.

* You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.

* You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.

* You can declare new methods in the subclass that are not in the superclass.

* You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

Page 52: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Private Members in a Superclass

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.

Page 53: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent.

Page 54: Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In

Creating and Using Interfaces

An interface is a collection of method definitions (without implementations) and constant values.