Upload
others
View
10
Download
0
Embed Size (px)
Citation preview
STATIC, ABSTRACT, AND INTERFACEThirapon Wongsaardsakul
Saturday, August 13, 11
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
STATIC VALUES
CLASS
Object
New
Using Attributes
and MethodsObject methods and variables
Using Attributes
and MethodsClass methods and variables
static
Saturday, August 13, 11
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
EXERCISE
• Create Circle and Football
• Observe static value
Saturday, August 13, 11
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
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
MAIN METHOD
• Explain why?
• public static void main(String[] args)
• Look at java.lang.Math class for static keyword
Saturday, August 13, 11
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
FINAL CLASS
• Preventing a user to create subclass
public final class Car { }
public class SportCar extends Car { } // Wrong
Saturday, August 13, 11
ANIMAL KINGDOMAnimal
Kingdom
Mammal Reptile Fish
Human Whale Monkey GuppyGold Fish
Saturday, August 13, 11
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
ANIMAL KINGDOMAnimal
Kingdom
Mammal Reptile Fish
HumanWhale Monkey GuppyGold
FishStudent
Saturday, August 13, 11
NORMAL CLASS
• Student is a class if we want it to be
Saturday, August 13, 11
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
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
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
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
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
EXERCISE
• Write Airplane.java and Bird.java that implements Flyer interface
Saturday, August 13, 11
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
EXAMPLE ON INTERFACE
Saturday, August 13, 11
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
ASSIGNMENT 6
• Chapter 5
• Programming assignment 4
Saturday, August 13, 11