abstract class and interface

10
Abstract class and Interface Group 3, Kompongsom Class

Upload: menghok-heak

Post on 22-Dec-2014

94 views

Category:

Education


2 download

DESCRIPTION

Understanding: - Abstract class - Interface - Differences

TRANSCRIPT

Page 1: Abstract Class and Interface

Abstract class and Interface

Group 3, Kompongsom Class

Page 2: Abstract Class and Interface

Member

• Leang Bunrong• Heak Menghok• Chea Socheat• Choun Sreynav• Buoy Sodeth

Page 3: Abstract Class and Interface

Content

• Abstract Class• Interface• Abstract class vs. Interface

Page 4: Abstract Class and Interface

Abstract Class

• Declared with abstract keyword• May or may not include abstract methods• Abstract class’s method can be public,

protected or default• Can not be instantiated• Can also be subclass• Non-abstract subclass must implement all

abstract methods of superclass

Page 5: Abstract Class and Interface

Abstract Class (cont.)

When use Abstract class?• Used to shared code to Related classes• When subclasses have many common

methods or fields• Provide default behavior (method)

Page 6: Abstract Class and Interface

Abstract Class (cont.)abstract class Animal {

public void eat(Food food) { /*Body statement*/ }public void sleep(int hours) {/*Body statement*/ }public abstract void makeNoise();

}

class Dog extends Animal{@Overridevoid makeNoise(){

System.out.println(“woof”);}

}

class Cat extends Animal{@Overridevoid makeNoise(){

System.out.println(“meow”);}

}

Page 7: Abstract Class and Interface

Interface

• Contains only Constants, method signatures• All fields in Interface are public, static and final• All methods in Interface are public and abstract• Can not be instantiated• Allow multiple implementations• Used when unrelated class using same name of

methods• Multi inheritance

Page 8: Abstract Class and Interface

Interface (cont.)interface IAnimal{

void walk();}

class Cat implements IAnimal{@Overridevoid walk(){

System.out.println(“Cat’s walking”);}

}

Page 9: Abstract Class and Interface

Differences

Abstract Class• Contains abstract/non-abstract

methods• Method can be public, protected,

default, private*• All field can be public, protected,

default, private, static or final• Single inheritance• Used when subclasses have many

common field/methods• Can be subclass

* non-abstract method only

Interface• Contains abstract methods

• Methods have to be public

• All fields are public static and final

• Multiple implementations• Used when classes using same

name of methods

Page 10: Abstract Class and Interface

Thank for paying Attention.

Questions?