0% found this document useful (0 votes)
8 views

Angular Basics 4

Uploaded by

vishalwdv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Angular Basics 4

Uploaded by

vishalwdv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Polymorphism:

class A{...}
How to use this class A?

1)creating object
let ob:A=new A();

2)by extending it
class B extends A{....}

but when we want the class only to be extending

abstract class A{
//data member
//non-abstract functions
//abstract methods (methods which has only declaration and no implementation)
abstract Fun1():void;

}
abstract class C {}
class B extends A
{
Fun1():void
{
}
}
//Abstract class is a class which cannot be instantiated but needs to be extended

interface IA
{
fun1():void;
fun2():void;
}
class MyClass implements IA
{
fun1() : void {}
fun2(): void{}
}
interface in Typescript
-----------------------
Interface is a pure abstract class

is a contract[set of specifications]

is a set of method declrations which needs to be implemented by some class

interface tells a class what to do ,not how to do

used to implement Polymorphism (Many Forms) : Compile Time & RunTime(samename &
same signature in base type and derived type)

Lab1)
ITune.ts
---------
create an interface ITune having a method Play()

Instruments.ts
--------------
write 3 classes Guitar,Piano & Violin implementing the interface ITune

CallerInstrument.ts
-------------------
create an array instruments:ITune[] holding Guitar,Piano & Violin objects & invoke
Play method

____________________________________________________________

Lab2)
Performer.ts
-------------
interface IPerformer{ Perform():void;}

Participants.ts
---------------
write 3 classes which implements interface
Juggler[beanBags]
Singer[song]
Magician[magicWords]

SeedIdol.ts
------------
create an array of IPerformer & invoke Perform method onit
let parr:Performer[]=[];
parr.push(new Juggler(5));
parr.push(new Singer("some song"));
parr.push(new Magician("some magic words"));
________________________________________________________

CaseStudy on Inheritance & Encapsulation using property fn


Lab3)
student.ts
-----------
Write a class Student[rollNo,name,marks-array of 5 subjects]
2 subclasses
PrimaryStudent[grade]
SecondaryStudent[percentage]

In all above 3 classes write constructor,PrintMarkSheet

callerStudent.ts
----------------
create an array of Students & invoke PrintMarkSheet method

Creating Properties:
private _eid:number;
get Eid:number { return _eid;} //getter method ------only get --
readonly property
set Eid(n1:number) { _eid = n1;} //setter method --------only set ---
writeonly

Writing a property has 2 parts - get and set


Declare a data member as private in class. Create getter and setter method for the
private variable.
For execution, we need to compile with
tsc --target ES5 file.ts
node file.js

Another way to hide the data members is to declare them inside constructor
Class A
{

constructor(private a:number)
{}
Display():void
{Console.log(this.a);}

obj:A = new A(100);


obj.Display();

THere are 3 access specifiers in typescript


1. Public
2.private
3.protected

class A
{
private a:number;
public b:number;
protected c:number;
}
class B extends A
{
Display():void
{
//access for b and c is possible
}
}

obj:A = new obj();


//check what is accessible

You might also like