TypeScript Abstract Class

Next Chapter »

In object oriented, abstract classes are base classes and cannot be instantiated directly. Abstract classes can have abstract and non-abstract methods. Methods that are marked as abstract do not have implementation and must be implemented by derived classes or sub classes. Abstract classes and methods can be created using abstract keyword within the abstract class.

Unlike interface:
An abstract class can give implementation details for its members.
An abstract class allows to marks its members methods as private and protected.

class classA {
    protected name: string;
    public constructor(name: string) 
    { 
     this.name = name;
    }
    public print() {
		return "Hello " + this.name;
    }
	abstract printAbstract(): void; // This must be implemented by derived classes
}
class classB extends classA {
    constructor(name: string) { super(name); }
	printAbstract(): void {
        console.log("Abstract method implemented by derived class");
	}
}

let b = new classB("Jimi Scott");
b.print();
b.printAbstract();

let classA = new classsA(); // error: cannot create an instance of an abstract class

In the above example, a new abstract class classA is created. This abstract class has declared one method printAbstract using abstract keyword. That means, this method must be implemented by the derived class. Here, class classB derived from classA and implementing printAbstract abstract method of base class. Also above example demonstrates, when creating object of abstract class error will be resulting.

Live Demo

Next Chapter »

See Also

AngularJS Built-in Filters
AngularJS Built-in Directives
AngularJS Expression
AngularJS Built-in Events
AngularJS Scope Methods
AngularJS Built-in Services
AngularJS Controller Inheritance
AngularJS Custom Directives



 
SHARE THIS IF YOU LIKE!
submit to reddit

TYPESCRIPT LANGUAGE TECHNIQUES

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.