24
STATIC, ABSTRACT, AND INTERFACE Thirapon Wongsaardsakul Saturday, August 13, 11

STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

STATIC, ABSTRACT, AND INTERFACEThirapon Wongsaardsakul

Saturday, August 13, 11

Page 2: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

STATIC

• When variable is a static type, java allocates memory for it at loading time.

• Class loader

• Byte code verifier

• Interpreter

• Runtime

Static variable has been loaded

Saturday, August 13, 11

Page 3: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

STATIC VALUES

CLASS

Object

New

Using Attributes

and MethodsObject methods and variables

Using Attributes

and MethodsClass methods and variables

static

Saturday, August 13, 11

Page 4: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

YOU CAN REFER TO CIRCLE.PI WITHOUT CREATE A OBJECT BUT NOT FOR A RADIUS

class Circle { static double PI = 3.1429; double radius = 5;}

Golfradius

Footballradius

Volleyballradius

PI = 3.1429

CDradius

Saturday, August 13, 11

Page 5: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

EXERCISE

• Create Circle and Football

• Observe static value

Saturday, August 13, 11

Page 6: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

EXAMPLE (DO IT IN BLUEJ)

\\ Create Student.java

public class Student {

static int numberOfStudent=0;

String name;

public Student(String name) {

this.name = name;

numberOfStudent++;

}

}

Saturday, August 13, 11

Page 7: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

STATIC METHODpublic class Student {

static int numberOfStudent=0;

String name;

public Student(String name) {

this.name = name;

numberOfStudent++;

}

public static void printNumberOfStudent() {

System.out.println(“There are “ + numberOfStudent + “ students”);

}

}

Saturday, August 13, 11

Page 8: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

MAIN METHOD

• Explain why?

• public static void main(String[] args)

• Look at java.lang.Math class for static keyword

Saturday, August 13, 11

Page 9: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

CONSTANT VALUE

• Keyword “final” is used to identify a constant variable

class Circle {

static final double PI = 3.1429;

double radius = 5;

}

Saturday, August 13, 11

Page 10: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

FINAL CLASS

• Preventing a user to create subclass

public final class Car { }

public class SportCar extends Car { } // Wrong

Saturday, August 13, 11

Page 11: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

ANIMAL KINGDOMAnimal

Kingdom

Mammal Reptile Fish

Human Whale Monkey GuppyGold Fish

Saturday, August 13, 11

Page 12: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

ABSTRACT CLASS

• Abstract class is just a definition of something that is not existed in a world

• Mammal, Reptile, and Fish are definition of something

• Human, Whale, Monkey, Gold fish, and Guppy are also definition

Saturday, August 13, 11

Page 13: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

ANIMAL KINGDOMAnimal

Kingdom

Mammal Reptile Fish

HumanWhale Monkey GuppyGold

FishStudent

Saturday, August 13, 11

Page 14: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

NORMAL CLASS

• Student is a class if we want it to be

Saturday, August 13, 11

Page 15: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

EXERCISE

abstract public class Animal {

abstract public void eat();

abstract public void sleep();

} You know that an animal has to eat and sleep But different animals, different eat and sleep

You cannot create object from Animal class

Write class Bird and Person which extends Animal

Saturday, August 13, 11

Page 16: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

EXTENDED EXERCISE

abstract public class Animal {

abstract public void eat();

abstract public void sleep();

public void die() {

System.out.println(“I died”);

}

How easy that you can add a functionality all of your classes

Saturday, August 13, 11

Page 17: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

INTERFACE CLASS

• Interface is used to apply a rule to a class

• Person class with Swimming interface

• Person class with PayTax interface

• Interface class is a pure abstract class

Saturday, August 13, 11

Page 18: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

INTERFACE = PURE ABSTRACT CLASS

• All variables in an interface are “public static final”

• All methods in an interface are “public abstract”

• However, you can ignore all these keywords

Saturday, August 13, 11

Page 19: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

HOW TO USE AN INTERFACEUsing keyword “implements” like extends

interface Flyer {

void takeOff();

void land();

void fly();

}

interface Flyer{ abstract public void takeOff(); abstract public void land(); abstract public void fly(); }

Saturday, August 13, 11

Page 20: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

EXERCISE

• Write Airplane.java and Bird.java that implements Flyer interface

Saturday, August 13, 11

Page 21: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

INHERITANCE AND INTERFACE

• Inheritance and interface are different things

• Inheritance represent “is-a” relationship

• Interface is a rule to apply gain functionalities to a class

Saturday, August 13, 11

Page 22: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

EXAMPLE ON INTERFACE

Saturday, August 13, 11

Page 23: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

CLASS VS. INTERFACE• Class

• Properties and methods definition

• “is-a” relationship: superclass-subclass

• Details of a method must be completed

• Only have one superclass

• Interface

• Rules definition

• Any classes can have the same interface

• Abstract methods are allowed

• Can implement many interfaces

Saturday, August 13, 11

Page 24: STATIC, ABSTRACT, AND INTERFACEtulip.bu.ac.th/~thirapon.w/gim/OOP_files/slide7.pdf · CLASS VS. INTERFACE • Class • Properties and methods definition • “is-a” relationship:

ASSIGNMENT 6

• Chapter 5

• Programming assignment 4

Saturday, August 13, 11