OOP - Inheritance

2 Types of Inheritance

  • extends (class)
  • implements (interface)
Type Extends (class) Implements (interface)
Have shared data?
Have shared behavior?
Enforce contract but still same form

Type of Inheritance

abstract class Cat{

//shared data 
private name: string;

public setName(name: string){
	this.name = name;
}

public getName(): string{
	return this.name;
}


//shared behavior
public meow(){
	console.log("meow")
}

//non shared behavior
//assuming cat fur have so many patterns,
//u unable to put it into params or you can put into params
//but the implementation differ so much for different cat
public abstract fur();

}//class cat

What is Shared data & behavior and enforce contract

Example: Inheritance extends / class

What is the best data structure to represent the ribbon menu?

abstract class MenuNode{
	//share data or shared behavior
    private label: string;
    private ID: number;
    private parentID: number;
    
    public getLabel(): string{
    	return this.string;
    }
    
    //non shared
    //non shared must use abstract
    //what kind of view to be displayed?
    public abstract getView(): MenuComponent;
    
    //call another window?
    //immediately do action?
    public abstract getAction(): MenuAction;

}
class MenuGroup extends MenuNode{
	constructor(label){
    	super(label);
    }
    
    public getView(): MenuComponent{
    	//just display veritcal line and its label
    }
}


class DropDownMenuItem extends MenuNode{
	
    public getView(): MenuComponent{
    	//just display name with drop down capabilities
    }
}

Example: implements / interface

Just Enforce Contract without changing Form

  • no shared data
  • no shared behaviors

Simple Data Management

To lower Case

Remove "a"

clamp "x"

Input: Nama saya Tom Jerry

nama saya tom jerry

n m s y tom jeryy

xn m s y tom jeryyx

interface Transformer{
	transform(input: string): string;
}


//OR

abstract class Transformer{
	abstract public transform(input: string): string;
}


//example use

class TransformToLowerCase implements Transformer{

	public transform(input: string): string{
    	return input.toLowerCase();
    }
}

OOP - inheritance

By Wan Razali

OOP - inheritance

  • 140