4
Classes https://docs.oracle.com/javase/tutorial/java/concepts/class.html class Bicycle { CLASS int cadence = 0; int speed = 0; INITIAL INTEGERS int gear = 1; void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } DECLARING TO INTEGERS void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { System.out.println("cadence:" + cadence + " speed:" + PRINT OUT speed + " gear:" + gear); } } Inheritance https://docs.oracle.com/javase/tutorial/java/concepts/ inheritance.html class MountainBike extends Bicycle { // new fields and methods defining

Java tutorials

Embed Size (px)

Citation preview

Page 1: Java tutorials

Classes

https://docs.oracle.com/javase/tutorial/java/concepts/class.html

class Bicycle { CLASS

int cadence = 0;

int speed = 0; INITIAL INTEGERS

int gear = 1;

void changeCadence(int newValue) {

cadence = newValue;

}

void changeGear(int newValue) {

gear = newValue;

} DECLARING TO INTEGERS

void speedUp(int increment) {

speed = speed + increment;

}

void applyBrakes(int decrement) {

speed = speed - decrement;

}

void printStates() {

System.out.println("cadence:" +

cadence + " speed:" + PRINT OUT

speed + " gear:" + gear);

}

}

Inheritance

https://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html

class MountainBike extends Bicycle {

// new fields and methods defining

// a mountain bike would go here

Can inherit traits from other forms of bikes

Page 2: Java tutorials

}

Interfaces

https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

Bicycle as Interface

interface Bicycle {

// wheel revolutions per minute

void changeCadence(int newValue);

void changeGear(int newValue);

void speedUp(int increment);

void applyBrakes(int decrement);

}

Page 3: Java tutorials

Implementing Interface must include: the methods (changeGear, changeCadence,

speedUp, applyBrakes, etc)

class BRANDBicycle implements Bicycle {

int cadence = 0;

int speed = 0; INITIAL VARIABLE

int gear = 1;

// The compiler will now require that methods

// changeCadence, changeGear, speedUp, and applyBrakes

// all be implemented. Compilation will fail if those

// methods are missing from this class.

void changeCadence(int newValue) {

cadence = newValue;

}

void changeGear(int newValue) {

gear = newValue;

}

void speedUp(int increment) {

speed = speed + increment;

}

void applyBrakes(int decrement) {

speed = speed - decrement;

}

void printStates() {

System.out.println("cadence:" +

cadence + " speed:" +

speed + " gear:" + gear);

}

}

Page 4: Java tutorials

Questions

1. Real-world objects contain State and Behaviour.

2. A software object's state is stored in ___.

3. A software object's behaviour is exposed through ___.

4. Hiding internal data from the outside world and accessing it only through publicly exposed

methods is known as data ___.

5. A blueprint for a software object is called a ___.

6. Common behaviour can be defined in a ___ and inherited into a ___ using the ___ keyword.

7. A collection of methods with no implementation is called a ___.

8. A namespace that organizes classes and interfaces by functionality is called a ___.

9. The term API stands for ___?