Before going into OOPS, we need to know about the Object based programming system
Object Based Programming System
Features: Supports code reusability and code separation
Support Dynamic Memory Allocation
Can be Integrated with other programming system
Issues:
It will not support Inheritance
It will not support dynamic polymorphism
Code Extensibiity Issues
Ex: Javascript , VB Script
Object Oriented Programming
Features
Code Reusability
Code Separation
Dynamic Memory Allocation
Code Extensibility
Issues
Uses more memory
Slow than other programming system
Complex in configuration
OOPS IN TypeScript
Object oriented programming is provided with 4 Synopsis
1 Data Encapsulation
Combing/Binding related fields and methods as one unit is Called as "Data Encapsulation"
Practical:
Class is an implementation of data Encapsulation.
2 Data Abstraction
Hiding secured Data and functionality from user with the help of access specifiers is Called "Data Abstraction"
Practical:
Object is an implementation of data Abstraction.
Class -->private a;
public b;
Object var:--> Only b will be accessible.
Object -->Memory is allocated for a and b.
Inheritance : Creating a new entity based on Existing Entity
Polymorphism:
Many forms of a single object is called as Polymorphism
Partically: method overloading and method overriding
SOLID Principles allows object oriented programming implementation in an efficient \better way into proper development.
S=Single Responsibility Principle [SRP]
O-Open Closed Principle
L-Liskov Substitution Principle
I-Interface Segregation Principle
D-Dependency Segregation Principle
Class in TypeScript:
Class is used to combine related Fields and methods as one unit.
TypeScript is providing "class" keyword to create a Class.
Syntax
class <className>
{
<accessspecifier> <fieldname>: <type>;
----------------------
-----------------------
constructor(para1:type,..........)
{
this.<fieldname>=para1;
-------------------------
-------------------------
}
<accessspecifier> <methodname>(para1,......) : <type>
{
-------------------------------
--------------------------------
}
}
A class is a logical representation of data. It is blue print or prototype that encapsulates a set of charactertics.
Object is physical representation of data. It is an instance of class that can access all characteristics of its class. Object can be created by using new keyword.
Events: Events is a message send by sender to it subscriber in order to notify the change. It specifies when it actions are to be performed.
A class is defined by using a keyword class
It encapsulates set of members by using " { } "
Every class by default contains a members called constructor
Constructor is a special type of method that is used by class to initialize values.
The name of the constructor will be same as class Name.
Constructor is invoked automatically , when an instance is created for class.
Note: The member of class are accessible within the class by using "this" keyword.
class Demo{public x:number;constructor(){this.x=10;}public print() : void {console.log("x=" + this.x);}}let obj=new Demo();obj.print();Output:PS C:\Examples> node classexample x=10Constructor
In TypeScript, the constructor method is always defined with the name "constructor".Creating Object of Class
let <objectvar>=new <classname>();
class Employee { empCode: number; empName: string; } let emp = new Employee();
Typescript supports three access specifiers
1 Private: Field /Method will be accessible only with in a class.
2 Protected: Field /Method will be accessible with in a class and derived class.
3 Public: Field /Method will be accessible in all the places
[Class, Derived Class and Object Variable ]
Default access specifier is public.
Default return type is void.
Note: There would be no default access specifier for class.
Syntax:
Class Base
{
public publicmember;
private privatemember;
protected protectedmember;
}
Class Derived extends Base {
public GetDetails(obj:Derived) {
obj.publicMember;
obj.protectedMember;
obj.privateMember; ---->Invalid not accessible
public ShowDetails(obj:Base)
obj.publicMember;
obj.protectedMember; --->Invalid not accessible
obj.privateMember; ---->Invalid not accessible
Example:
class calc{private _a:number;private _b:number;private _res:number;constructor(a:number,b:number){this._a=a;this._b=b;}sum():number{this._res=this._a+this._b;return this._res;}public mult():number{this._res=this._a*this._b;return this._res;}}let obj=new calc(20,30);console.log("sum:"+obj.sum()+" mult:"+obj.mult());
No comments:
Post a Comment
Thank you for visiting my blog