Java OOP: (SE Tutorials: Learning The Java Language Trail: Object-Oriented Programming Concepts Lesson)
Java OOP: (SE Tutorials: Learning The Java Language Trail: Object-Oriented Programming Concepts Lesson)
Dongwon Jeong
djeong@kunsan.ac.kr; http://ist.kunsan.ac.kr/
Contents
Scope
OOP Concepts: Key Parts
What Is an Object?
What Is a Class?
What Is Inheritance?
What Is an Interface?
What Is a Package?
Q/A
Ref.
9 The Java Tutorials
Æ Trail: Learning the Java Language
9 http://java.sun.com/docs/books/tutorial/java/index.html
This trail covers the fundamentals of programming in the Java
programming language
9 Object-Oriented Programming Concepts
teaches you the core concepts behind object-oriented programming:
objects, messages, classes, and inheritance. This lesson ends by
showing you how these concepts translate into code. Feel free to skip
this lesson if you are already familiar with object-oriented programming.
9 Language Basics
describes the traditional features of the language, including variables,
arrays, data types, operators, and control flow.
9 Classes and Objects
describes how to write the classes from which objects are created, and
how to create and use the objects.
Scope (cont.)
What Is Inheritance?
9 Inheritance provides a powerful and natural mechanism for organizing
and structuring your software.
9 This section explains how classes inherit state and behavior from their
superclasses, and explains how to derive one class from another using
the simple syntax provided by the Java programming language.
What Is an Interface?
9 An interface is a contract between a class and the outside world.
9 When a class implements an interface, it promises to provide the
behavior published by that interface.
9 This section defines a simple interface and explains the necessary
changes for any class that implements it.
What Is a Package?
9 A package is a namespace for organizing classes and interfaces in a
logical manner.
9 Placing your code into packages makes large software projects easier
to manage.
9 This section explains why this is useful, and introduces you to the
Application Programming Interface (API) provided by the Java platform.
What Is an Object?
Objects
9 key to understanding object-oriented technology.
9 Examples of real-world objects: your dog, your desk, your television set,
your bicycle.
9 Real-world objects share two characteristics
– They all have state and behavior
– Dogs have state (name, color, breed, hungry) and behavior (barking,
fetching, wagging tail).
– Bicycles also have state (current gear, current pedal cadence, current
speed) and behavior (changing gear, changing pedal cadence, applying
brakes).
What Is a Class?
class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
You may have noticed that the Bicycle class does not contain a
main method.
9 That's because it's not a complete application
9 It's just the blueprint for bicycles that might be used in an application.
9 The responsibility of creating and using new Bicycle objects belongs to
some other class in your application.
bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}
What Is an Interface?
interface Bicycle {
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
To implement this interface, the name of your class would change (to
ACMEBicycle, for example), and you'd use the implements keyword in
the class declaration:
Implementing an interface
9 allows a class to become more formal about the behavior it promises to
provide.
Interfaces
9 form a contract between the class and the outside world, and
9 this contract is enforced at build time by the compiler
If your class claims to implement an interface,
9 all methods defined by that interface must appear in its source code
9 before the class will successfully compile
Note:
9 To actually compile the ACMEBicycle class,
9 you'll need to add the public keyword to the beginning of the
implemented interface methods.
9 You'll learn the reasons for this later in the lessons on Classes and
Objects and Interfaces and Inheritance.
What Is a Package?
A package
9 is a namespace that organizes a set of related classes and interfaces
9 Conceptually you can think of packages as being similar to different
folders on your computer.
9 You might keep HTML pages in one folder, images in another, and
scripts or applications in yet another.
9 Because software written in the Java programming language can be
composed of hundreds or thousands of individual classes, it makes
sense to keep things organized by placing related classes and
interfaces into packages.
Dongwon Jeong
djeong@kunsan.ac.kr; http://ist.kunsan.ac.kr
Information Sciences & Technology Laboratory,
Informatics & Statistics Department,
Kunsan National University