Svetlin Nakov Telerik Corporation www.telerik.com.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
More about classes and objects Classes in Visual Basic.NET.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
Advanced Object-Oriented Programming Features
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
2-1 © Prentice Hall, 2004 Chapter 2: Introduction to Object Orientation (Adapted) Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra,
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Comparison of OO Programming Languages © Jason Voegele, 2003.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 12: Adding Functionality to Your Classes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
An Object-Oriented Approach to Programming Logic and Design
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
CONCEPTS OF OBJECT ORIENTED PROGRAMMING. Topics To Be Discussed………………………. Objects Classes Data Abstraction and Encapsulation Inheritance Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
JavaScript, Fourth Edition
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Inheritance Building one object from another. Background Object-oriented programming is normally described has offering three capabilities Encapsulation:
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.
Svetlin Nakov Telerik Corporation
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Prototype Chain and Inheritance Prototype chain, Inheritance, Accessing Base Members Software University Technical Trainers SoftUni Team.
By : Robert Apeldorn. What is OOP?  Object-oriented programming is a programming paradigm that uses “objects” to design applications and computer programs.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Simulating OOP in JavaScript Function Constructor, Prototypes, "this" Object, Classical and Prototypal Model Software University Technical.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Object Oriented JavaScript. JavaScript Really only 4 types in JavaScript – number, string, boolean – Object (includes arrays) Remember that an object.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
ISBN Chapter 12 Support for Object-Oriented Programming.
Object Oriented Programming in Java Habib Rostami Lecture 2.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
OOP: Encapsulation &Abstraction
The Movement To Objects
Inheritance and Polymorphism
One class is an extension of another.
3 Fundamentals of Object-Oriented Programming
Object Oriented Analysis and Design
Chapter 10 Thinking in Objects
One class is an extension of another.
JavaScript Objects
Object-Oriented Programming
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

Svetlin Nakov Telerik Corporation

 JavaScript OOP  Constructors  Properties  Functions  Inheritance  Polymorphism  Extending Prebuilt JavaScript Objects 2

Properties, Functions, Inheritance 3

 The current design of the JavaScript language, did not fully implement the object-oriented paradigms  There are various implementations of object- oriented programming techniques being used on the Web today  Primary goals of OOP  Encapsulation  Polymorphism  Inheritance

 The simplest way is to use the built-in Object data type  In JavaScript, objects are implemented as a collection of named properties (key-value pairs)  JavaScript allows the creation of any number of properties in an object at any time  They are dynamic – do not have to be pre-defined in an object declaration or constructor 5 var student = new Object; student.name= "Doncho Minkov"; student.grade = 3;

 A new JavaScript class is defined by creating a function (serving as constructor)  When used with the new operator, a function serves as a constructor for that class  Internally, JavaScript creates an Object, and then calls the constructor function 6 function Student() { this.name = "Doncho Minkov"; this.name = "Doncho Minkov"; this.grade = 3; this.grade = 3;} var student = new Student;

 When defining a constructor function, we can make many objects with the same properties 7 function Student(name, grade) { this.name = name; this.name = name; this.grade = grade; this.grade = grade;} var doncho = new Student("Doncho Minkov", 3); var pesho = new Student("Pesho Peshov",2 ); var stamat = new Student("Stamat Geshov",4);

 We can add a functions (methods) to the class at any time 8 function Student(name, grade) { this.name = name; this.name = name; this.grade = grade; this.grade = grade; this.sayHello = function() { alert("Hi! I am " + this.name); this.sayHello = function() { alert("Hi! I am " + this.name); }} var doncho = new Student("Doncho Minkov", 3); doncho.sayHello(); defining-classes.html

9

 We can use the prototype object to add custom properties / methods to classes  That is reflected on all instances of the class  How to use the prototype object?  Simply reference the keyword prototype on the object before adding the custom property 10 function Circle() { } Circle.prototype.pi = ;

 Adding a function to a class at runtime using the prototype object 11 function Circle() { } Circle.prototype.pi = ; Circle.prototype.radius = 5; Circle.prototype.calculateArea = function () { return this.pi * this.radius * 2; return this.pi * this.radius * 2;} var circle = new Circle(); var area = circle.calculateArea(); alert(area); // prototype-object.html

 Dynamically add a function to a built-in class at runtime using the prototype object: 12 Array.prototype.showMax = function () { function () { var max = this[0]; var max = this[0]; for (var i = 1; i < this.length; i++) { for (var i = 1; i < this.length; i++) { if (max < this[i]) { if (max < this[i]) { max = this[i]; max = this[i]; } } return max; return max; } var array = new Array(9, 1, 11, 3, 4); var max = array.showMax(); alert(max); // 11 Attaching a method to the Array class

 To inherit a class in JavaScript you should set the prototype object of the subclass to the superclass class: 14 function Person(name) { this.name = name; this.name = name; this.talk = function () { this.talk = function () { alert("Hi! I am " + this.name); alert("Hi! I am " + this.name); }} function Student(name, grade) { this.name = name; this.name = name; this.grade = grade; this.grade = grade;} Student.prototype = new Person(); This way we say that the Student class will have all the functionality of the Person class inheritance.html

 Polymorphism = ability to take more than one form (objects have more than one type)  A class can be used through its parent interface  A child class may override some of the behavior of the parent class 15 Student.prototype = new Person(); Teacher.prototype = new Person(); var array = new Array( new Teacher("Gana","Math"), new Student("Gosho",3), new Teacher("Gana","Math"), new Student("Gosho",3), new Person("Pesho"), new Teacher("Mara","Literature")); new Person("Pesho"), new Teacher("Mara","Literature")); for (var i = 0; i < array.length; i++) { array[i].talk(); array[i].talk();} polymorphism.html

Questions?Questions? 16

 Implement a class Human, having name, gender, address, telephone number  It should have a methods for introducing himself (ex. "Hi I am …!", "I am … years old!")  Implement classes Student and Parent inheriting the Human class  A Student should have  State holding where s/he studies, a list of his/her marks  A method to count the average of their marks  A method for adding/removing a mark  A Parent should hold a list of all his children(Student objects) and a method to yell at a concrete of his children