Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad.

Slides:



Advertisements
Similar presentations
Windows Basic and Dynamic Disk Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Advertisements

Make swiftly iOS development Telerik Academy Telerik Academy Plus.
Amazon S 3, App Engine Blobstore, Google Cloud Storage, Azure Blobs Svetlin Nakov Telerik Software Academy academy.telerik.com.
RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Telerik Software Academy Telerik School Academy.
Asynchronous Programming with C# and WinRT
Unleash the Power of JavaScript Tooling Telerik Software Academy End-to-end JavaScript Applications.
Touch and Gestures with Xamarin Forms
Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy.
Hybrid or Native?! Doncho Minkov Telerik Software Academy Senior Technical Trainer
Done already for your convenience! Telerik School Academy Unity 2D Game Development.
Processing Sequences of Elements Telerik School Academy C# Fundamentals – Part 1.
With Mocha and Karma Telerik Academy Telerik Software Academy.
C# Fundamentals – Part I
Telerik Software Academy Telerik School Academy Creating E/R Diagrams with SQL Server.
The Business Plan and the Business Model Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik Corporation.
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Making JavaScript code by template! Learning & Development Team Telerik Software Academy.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training Who, What, Why?
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Learning & Development Telerik Software Academy.
2.5 OOP Principles Part 1 academy.zariba.com 1. Lecture Content 1.Fundamental Principles of OOP 2.Inheritance 3.Abstraction 4.Encapsulation 2.
Reading and Writing Text Files Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Telerik Software Academy ASP.NET Web Forms.
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Optimization problems, Greedy Algorithms, Optimal Substructure and Greedy choice Learning & Development Team Telerik Software.
Using Selenium for Mobile Web Testing Powered by KendoUI Telerik QA Academy Atanas Georgiev Senior QA Engineer KendoUI Team.
New features: classes, generators, iterators, etc. Telerik Academy Plus JavaScript.Next.
Svetlin Nakov Telerik Corporation
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
When and How to Refactor? Refactoring Patterns Telerik Software Academy High-Quality Code.
Loops, Conditional Statements, Functions Tran Anh Tuan Edit from Telerik Academy
Concerned with communication (interaction) between the objects Telerik Software Academy High-Quality Code.
Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.
Building Data-Driven ASP.NET Web Forms Apps Telerik Software Academy ASP.NET Web Forms.
Course Introduction Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
General and reusable solutions to common problems in software design Vasil Dininski Telerik Software Academy academy.telerik.com Intern at Telerik Academy.
Planning and Tracking Software Quality Yordan Dimitrov Telerik Corporation Team Leader, Team Pulse, Team Leader, Team Pulse, Telerik Corporation,
What you need to know Ivaylo Kenov Telerik Corporation Telerik Academy Student.
Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Pavel Kolev Telerik Software Academy Senior.Net Developer and Trainer
Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Doing the Canvas the "easy way"! Learning & Development Telerik Software Academy.
Inheritance, Abstraction, Encapsulation. 1. Fundamental Principles of OOP 2. Inheritance  Class Hierarchies  Inheritance and Access Levels 3. Abstraction.
Creating and Running Your First C# Program Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Correctly Formatting the Source Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
The past, the present, the future Learning & Development Team Telerik Software Academy.
Learn to Design Error Steady Code Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Processing Sequences of Elements Telerik Software Academy C# Fundamentals – Part 2.
Telerik JavaScript Framework Telerik Software Academy Hybrid Mobile Applications.
Telerik Software Academy Databases.
Polymorphism, Class Hierarchies, Exceptions, Strong Cohesion and Loose Coupling Telerik Software Academy Object-Oriented Programming.
Things start to get serious Telerik Software Academy JavaScript OOP.
Learning & Development Mobile apps for iPhone & iPad.
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
Mocking tools for easier unit testing Telerik Software Academy High Quality Code.
What why and how? Telerik School Academy Unity 2D Game Development.
Windows Security Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Presentation transcript:

Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad

 OOP Principles  Inheritance  Protocols  Abstraction  Encapsulation 2

 Inheritance  Inherit members from parent class  Abstraction  Define and execute abstract actions  Encapsulation  Hide the internals of a class  Polymorphism  Access a class through its parent interface 4

 Classes define attributes and behavior  Fields, properties, methods, etc.  Methods contain code for execution  Protocols define a set of operations  Empty methods and properties, left to be implemented later @implementation

 Inheritance allows classes to inherit characteristics from an existing parent (super) class  Attributes (fields and properties)  Operations (methods)  A child class can extend the parent class  Add new fields and methods  Redefine methods (modify existing behavior)  A class can conform to a protocol by providing implementation for its methods 7

 Inheritance terminology derived class base class / parent class inherits classprotocol conforms to 8

 Inheritance has a lot of benefits  Extensibility  Reusability (code reuse)  Provides abstraction  Eliminates redundant code  Use inheritance for buidling is-a relationships  E.g. person is-a mammal  Don't use it to build has-a relationship  E.g. person has-a name 9

 Child classes implicitly gain all members from the super class  All fields, methods, properties  Some members are inaccessible (hidden)  The class whose methods are inherited is called base (parent) class  The class that gains new functionality is called derived (child) class 10

11 Person +Name: NSString +Address: NSString Employee +Company: NSString +Salary: double Student +School: NSString Base class Derived class

 Inheritance leads to a hierarchies of classes and / or protocols in an application: 12 Game MultiplePlayersGame BoardGame Chess Backgammon SinglePlayerGame Minesweeper Solitaire … …

 A class can inherit only one base class  E.g. NSMutableArray derives from NSArray  A class can conform to several protocols  This is Obj-C’s form of multiple inheritance  Shape conforms to Movable and Drawable 13

 Specify the name of the base class after the name of the derived (with colon) Person: (strong, nonatomic) NSString* (strong, nonatomic) Ninja: int

 Specify the name of the base class after the name of the derived (with colon) Person: (strong, nonatomic) NSString* (strong, nonatomic) Ninja: int Inherits properties firstname and lastname from Person

 Specify the name of the base class after the name of the derived (with colon) Person: (strong, nonatomic) NSString* (strong, nonatomic) Ninja: int Inherits properties firstname and lastname from Person Adds new property

@interface Person: NSObject -(instancetype) initWithFirstname: (NSString *) fname andLastname: (NSString *) lname; andLastname: (NSString *) lname; -(void) introduce; -(void) lastname; -(instancetype) initWithFirstname: (NSString *) fname andLastname: (NSString *) lname{ andLastname: (NSString *) lname{} -(void) introduce{ } -(void) walk{

@interface Ninja: int rank; -(instancetype) initWithRank: (int) rank; -(void) rank; -(instancetype) initWithRank: (int) rank{ self = [super self = [super self.rank = rank; self.rank = rank; return self; return self;} -(void) walk{ } -(void) fight{

@interface Ninja: int rank; -(instancetype) initWithRank: (int) rank; -(void) rank; -(instancetype) initWithRank: (int) rank{ self = [super self = [super self.rank = rank; self.rank = rank; return self; return self;} -(void) walk{ } -(void) fight{ Adds new property – rank Adds new method – fight

@interface Ninja: int rank; -(instancetype) initWithRank: (int) rank; -(void) rank; -(instancetype) initWithRank: (int) rank{ self = [super self = [super self.rank = rank; self.rank = rank; return self; return self;} -(void) walk{ } -(void) fight{ Adds new property – rank Adds new method – fight Overwrites the method from the parent

Live Demo

 In Objective-C there is no multiple inheritance  Yet, a class can conform to multiple protocols  Class members are also inherited  Init methods are inherited  Inheritance is transitive relation  If C is derived from B, and B is derived from A, then C inherits A as well 22

 When a derived class extends its base class  It can freely add new members  Cannot remove derived ones  Declaring new members with the same name or signature overwrites the inherited ones  A class may not provide implementation to some methods  Derived classes can provide the implementation 23

 Abstraction means ignoring irrelevant features, properties, or functions and emphasizing the relevant ones... ... relevant to the given project  With an eye to future reuse in similar projects  Abstraction helps managing complexity "Relevant" to what? 25

 Abstraction is something we do every day  Looking at an object, we see those things about it that have meaning to us  We abstract the properties of the object, and keep only what we need  E.g. students get "name" but not "color of eyes"  Allows us to represent a complex reality in terms of a simplified model  Abstraction highlights the properties of an entity that we need and hides the others 26

 In Objective-C object-oriented programming abstraction is achieved in several ways:  Protocols  Inheritance +Color : long ButtonBase +click() Control ButtonRadioButton CheckBox 27

 An protocol defines a set of messages (methods) that given object should perform  Also called "contract" for providing a set of operations  Defines abstract behavior  Protocols provide abstractions  You invoke the abstract actions  Without worrying how it is internally implemented 28

 Protocols describe a prototype of group of methods (operations) or properties  Can be conformed by a given class  Define only the prototypes of the operations  No concrete implementation is provided  Can be used to define abstract data types  Can not be instantiated  Can contain optional and required members 29

double double y; -(void) moveToX:(double) x andY: (double) y; -(double) calculateSurface; double double height -(void) resizeWidth: (double) width; -(void) resizeHeight: (double) height; -(void) resizeWidth: (double) width andHeight: (double) weighty; andHeight: (double)

 Classes can conform to one or several protocols Rectangle : Rectangle : NSObject -(instancetype) initWithX: (double) x y: (double) y y: (double) y width: (double) width width: (double) width andHeight: (double) height; andHeight: (double)  Implementer classes must conform all methods and properties of the protocol

Rectangle: Rectangle: NSObjet // Rectangle height; -(void) moveToX:(double) x andY: (double) y { /* implementation */ } andY: (double) y { /* implementation */ } -(double) calculateSurface { /* implementation */ } -(double) calculateArea { /* implementation */ } -(void) resizeWidth: (int) width { /* implementation */ } -(void) resizeHeight: (int) height { /* implementation */ } -(void) resizeWidth: (int) width andHeight: (int) weighty { /* implementation */ } andHeight: (int) weighty { /* implementation */

Live Demo

 Encapsulation hides the implementation details  Class announces some operations (methods) available for its clients – its public interface  All data members (fields) of a class should be hidden  Accessed via properties (read-only and read- write)  No interface members should be hidden 35

 Data fields are private  Init methods and accessors are defined (getters and setters) Person -_name : NSString -_age : int +initWith: (NSString*) name andAge: (int) age andAge: (int) age +name : NString +age : int 36

 Fields are always declared private  They cannot be public anyway  Init methods are almost always declared public  Protocol members are always public  Non-interface members are declared private / protected 37

 Ensures that structural changes remain local:  Changing the class internals does not affect any code outside of the class  Changing methods' implementation does not reflect the clients using them  Encapsulation allows adding some logic when accessing client's data  E.g. validation on modifying a property value  Hiding implementation details reduces complexity  easier maintenance 38

Live Demo

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране

 Implement classes for a simple Mortal Kombat game  Create characters that have name, set of skills, life and power  Different characters have different skills  Characters can punch, kick or use a skill  They cannot use a skill, if the power is not enough  Kicks and punches take damage and product power  Skills take damage and consume part of the power of the character

 (Cont.) Implement classes for a simple Mortal Kombat game  Create 5 different characters with different skills  Use protocols and inheritance  Write a method to test the functionality  Use abstraction, encapsulation and polymorphism

 C# Telerik Academy  csharpfundamentals.telerik.com csharpfundamentals.telerik.com  Telerik Software Academy  academy.telerik.com academy.telerik.com  Telerik Facebook  facebook.com/TelerikAcademy facebook.com/TelerikAcademy  Telerik Software Academy Forums  forums.academy.telerik.com forums.academy.telerik.com