oops characteristics (with examples in php)

36

Upload: baabtracom-mentoring-partner-first-programming-school-in-india

Post on 19-Jan-2015

2.786 views

Category:

Technology


5 download

DESCRIPTION

OOPS Characteristics (With Examples in PHP)

TRANSCRIPT

Page 1: OOPS Characteristics (With Examples in PHP)
Page 2: OOPS Characteristics (With Examples in PHP)

OOPS CONCEPTS

Rajishma [email protected]/ Rajishma T

Nairtwitter.com/usernamein.linkedin.com/in/profilename9020217968

Page 3: OOPS Characteristics (With Examples in PHP)

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Page 4: OOPS Characteristics (With Examples in PHP)

CLASS A class is an expanded concept of a data,it

can hold both data and functions.A class is the blueprint for your object.A class, for example, is like a blueprint for a

house. It defines the shape of the house on paper, with relationships between the different parts of the house clearly defined and planned out, even though the house doesn’t exist.

Page 5: OOPS Characteristics (With Examples in PHP)

visibilityThe visibility of class members, (properties,

methods), relates to how that member may be manipulated within, or from outside the class.

private:private members are accessible only in the class itself.

Public:public members are accessible outside the class(anywhere).

Protected:protected members are accessible in the same package,and the subclasses of the class and inside the class.

Page 6: OOPS Characteristics (With Examples in PHP)

OOPS CHARACTERESTICS

data encapsulation. inheritance.Polymorphism.Data abstraction.

Page 7: OOPS Characteristics (With Examples in PHP)

ENCAPSULATION

The wrapping up of a data and functions into a single unit(which is called class).

Encapsulation means that some or all of an objects internal structure is hidden from the outside world.

Hidden information may only be accessed through the object’s public interface.

Page 8: OOPS Characteristics (With Examples in PHP)

example<?phpclass MyClass{ public $prop1 = "I'm a class property!";

public function setProperty($newval) { $this->prop1 = $newval; } public function getProperty() { return $this->prop1 . "<br />"; }}$obj = new MyClass;echo $obj->getProperty(); // Get the property value$obj->setProperty("I'm a new property value!"); // Set a new oneecho $obj->getProperty(); // Read it out again to show the change?>Output• http://localhost/phpWorkspace/oops.php/class.php

Page 9: OOPS Characteristics (With Examples in PHP)

INHERITANCE• When you want to create a new class and there

is already a class that includes some of the code that you want, you can derive your new class from the existing class.

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

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

Page 10: OOPS Characteristics (With Examples in PHP)

TYPES OF INHERITANCE

• Single Inheritance • Hierarchical Inheritance • Multi Level Inheritance • Hybrid Inheritance • Multiple Inheritance

Page 11: OOPS Characteristics (With Examples in PHP)

SINGLE INHERITANCE

• when a single derived class is created from a single base class then the inheritance is called as single inheritance.

Page 12: OOPS Characteristics (With Examples in PHP)

HIERARCHICAL INHERITANCE

• when more than one derived class are created from a single base class, then that inheritance is called as hierarchical inheritance.

Page 13: OOPS Characteristics (With Examples in PHP)

MULTILEVEL INHERITANCE

• when a derived class is created from another derived class, then that inheritance is called as multi level inheritance.

Page 14: OOPS Characteristics (With Examples in PHP)

HYBRID INHERITANCE

• Any combination of single, hierarchical and multi level inheritances is called as hybrid inheritance.

Page 15: OOPS Characteristics (With Examples in PHP)

MULTIPLE INHERITANCE

• when a derived class is created from more than one base class then that inheritance is called as multiple inheritance.

Page 16: OOPS Characteristics (With Examples in PHP)

Single inheritance<?phpclass parentclass { public function sum($a,$b) { echo $a+$b; }}class childclass extends parentclass{ public function diff($a,$b) { echo $a-$b; }}$obj1=new childclass();$obj1->sum(4,5);echo "<br />";echo "<br />";$obj1->diff(8,3);?>Output:http://localhost/phpWorkspace/oops.php/inheritance1.php

Page 17: OOPS Characteristics (With Examples in PHP)

Hierarchical inheritance<?phpclass a{ public function function_a() { echo "classA"; }}class b extends a{ public function function_b() { echo "classB"; }}class c extends a{ public function function_c() { echo "classC";}}

Page 18: OOPS Characteristics (With Examples in PHP)

echo c::function_c();echo "<br />";echo c::function_a();echo “<br />echo b::function_a();

?>

• Output

http://localhost/phpWorkspace/oops.php/hierarchicalinheritance.php

Page 19: OOPS Characteristics (With Examples in PHP)

Multilevel inheritance

<?phpclass a{ public function function_a(){ echo 'classa';}}class b extends a{public function function_b(){echo 'classb';}}class c extends b{public function function_c(){echo 'classc';}}

Page 20: OOPS Characteristics (With Examples in PHP)

echo c::function_c();echo "<br />";eCho c::function_b();echo "<br />";echo c::function_a();

?>OUTPUT

http://localhost/phpWorkspace/oops.php/multilevel.php

Page 21: OOPS Characteristics (With Examples in PHP)

POLYMORPHISM

• Poly means many,morphism means forms.• Polymorphism feature enables classes to

provide different implementation of methods having the same name.

• Two types of polymorphismCompile time(overloading)Runtime(overriding)

Page 22: OOPS Characteristics (With Examples in PHP)

OVERRIDING• When we create a function in a derived class

with the same signature (in other words a function has the same name, the same number of arguments and the same type of arguments) as a function in its parent class then it is called method overriding.

Page 23: OOPS Characteristics (With Examples in PHP)

example

<?phpclass Shap{ function draw(){}} class Circle extends Shap{ function draw() { print "Circle has been drawn.</br>"; }}class Triangle extends Shap{ function draw() { print "Triangle has been drawn.</br>"; }}class Ellipse extends Shap{ function draw() { print "Ellipse has been drawn."; }}

}

Page 24: OOPS Characteristics (With Examples in PHP)

$Val=array(2);$Val[0]=new Circle();$Val[1]=new Triangle();$Val[2]=new Ellipse();for($i=0;$i<3;$i++) { $Val[$i]->draw();}?>

• Outputhttp://localhost/phpWorkspace/oops.php/overriding.php

Page 25: OOPS Characteristics (With Examples in PHP)

OVERLOADING

• Method overloading means having two or more methods with the same name but different signatures in the same scope. These two methods may exist in the same class or another one in base class and another in derived class.

Page 26: OOPS Characteristics (With Examples in PHP)

Example<?phpfunction findSum() { $sum = 0; foreach (func_get_args() as $arg) { $sum += $arg; } return $sum;}echo findSum(1, 2), '<br />'; echo findSum(10, 2, 100), '<br />'; echo findSum(10, 22, 0.5, 0.75, 12.50), '<br />'; ?>Output:http://localhost/phpWorkspace/oops.php/overloading.php

Page 27: OOPS Characteristics (With Examples in PHP)

DATA ABSTRACTION

• Any representation of data in which the implementation details are hidden (abstracted).

• Data abstraction refers to, providing only essential information to the outside word and hiding their background details ie. to represent the needed information in program without presenting the details

• They provide sufficient public methods to the outside world to play with the functionality of the object and to manipulate object data ie. state without actually knowing how class has

been implemented internally.

Page 28: OOPS Characteristics (With Examples in PHP)

ABSTRACT CLASS

• An abstract class is one that cannot be instantiated, only inherited. You declare an abstract class with the keyword abstract

• When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child;

Page 29: OOPS Characteristics (With Examples in PHP)

<?phpabstract class One{abstract function disp();}class Two extends One {public function disp() {echo "Inside the child class<br/>";}}class Three extends One{public function disp(){echo "Inside the child class 2<br/>";}}

Page 30: OOPS Characteristics (With Examples in PHP)

$two=new Two();echo "<b>Calling from the child class Two:</b><br/>";$two->disp();echo "<b>Calling from the child class Three:</b><br/>";$three=new Three();$three->disp();?>Output

http://localhost/phpWorkspace/oops.php/abstractclass.php

Page 31: OOPS Characteristics (With Examples in PHP)

INTERFACE

• Interface is a special class used like a template for a group of classes with similar functions, which must define a certain structure of methods.

• It can contain constants and method declarations, but not method bodies.

Page 32: OOPS Characteristics (With Examples in PHP)

<?php interface testdrive { function drive(); function stop(); }class vehicle implements testdrive{ public function __construct() { echo 'About this Vehicle.<br />'; } public function drive() { echo 'VRRROOOOOOM!!!'; }

public function stop() { echo 'SSSCCRRREEEEEECCHH!!!<br />'; }}

Page 33: OOPS Characteristics (With Examples in PHP)

$object = new vehicle;$object->drive();$object->stop();?>

Outputhttp://localhost/phpWorkspace/oops.php/interface.php

Page 34: OOPS Characteristics (With Examples in PHP)

ABSTRACT CLASS INTERFACE

The Abstract methods can declare with Access modifiers like public, internal, protected. When implementing in subclass these methods must be defined with the same (or a less restricted) visibility.

All methods declared in an interface must be public.

Abstract class can contain variables and concrete methods.

Interfaces cannot contain variables and concrete methods except constants.

A class can Inherit only one Abstract class and Multiple inheritance is not possible for Abstract class.

A class can implement many interfaces and Multiple interface inheritance is possible.

Page 35: OOPS Characteristics (With Examples in PHP)

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 36: OOPS Characteristics (With Examples in PHP)

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com