Inheritance

Lectures 20-21

class Circle {

Circle(){. . .}

Circle(int radii) {. . .}

protected void draw() {…}

protected void rotate(int ang){}

}

class FilledCircle extends Circle {

FilledCircle() {…}

FilledCircle(int radii) {

super(i);

… }

protected void draw() {…}

protected void rotate(int ang){...}

}

Shape S; Circle C;

S=C;

S.draw();

class Shape {

Shape();

Shape(int dist);

protected void draw();

protected void rotate(int angle);

}

class Circle extends Shape {

Circle() {. . .}

Circle(int radii) {

super(i); … }

protected void draw() {…}

protected void rotate(int angle){}

}

A.foo(); // implies B.foo()

class Circle { . . . }

class FilledCircle extends Circle {

. . .

protected void fillPattern () { … }

}

Circle C = new FilledCircle(); // by substitution

C.fillPattern();

(FilledCircle)C.fillPattern(); //OK

void fillPattern (class FilledCircle {

FilledCircle() {…}

protected private void fillPattern () { … } }

class NoFilledCircle extends FilledCircle {

NoFilledCircle() {…}

protected ) { … } }

FilledCircle FC;

NoFilledCircle nFC = new NoFilledCircle();

FC = nFC;

nFC.fillPattern(); // compilation error

 FC.fillPattern(); // runtime exception

package Java.lang.Thread

//implementing Thread by extending Thread class

public class MyThread extends Thread{

public void run(){

System.out.println(“CThread"+Thread.currentThread().getName());

} }

//implementing Thread by implementing Runnable interface

public class MyRunnable implements Runnable{

static int myCount = 0;

public void run(){ ++MyRunnable.myCount;

System.out.println(“IThread" + Thread.currentThread().getName());

}}

Thread mythread = new MyThread(); mythread.setName("Th1");

Thread myrunnable = new Thread(new MyRunnable(),"Th2");

// or any constructor

mythread.start();         //Thread started now but not running

myrunnable.start();         // start depends on scheduler