



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




1) What Is an Object? An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life. This lesson explains how state and behavior are represented within an object, introduces the concept of data encapsulation, and explains the benefits of designing your software in this manner.
2) What Is a Class? A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.
3) What Is Inheritance? Inheritance provides a powerful and natural mechanism for organizing and structuring your software. 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.
4) What Is an Interface? An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface. This section defines a simple interface and explains the necessary changes for any class that implements it.
5) What Is a Package? A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage. 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 are a key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your car, your television set, your mobile phone. Real-world objects share two characteristics: They all have state and behavior. Car also has state (current speed, current gear, current power) and behavior (changing speed, changing gear, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.
A Software Object.
Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. By attributing state (current speed, current gear, and current power) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the car only has 4 gears, a method to change gears could reject any value that is less than 1 or greater than 4.
represent the object's state, and the methods (changeSpeed, changeGear, speedUp etc.) define its interaction with the outside world. You may have noticed that the Car class does not contain a main method. That's because it's not a complete application; it's just the blueprint for car that might be used in an application. The responsibility of creating and using new Car objects belongs to some other class in your application. Here's a CarDemo class that creates two separate Car objects and invokes their methods:
class CarDemo { public static void main(String[ ] args) { // Create two different Car objects Car car1 = new Car( ); Car car2 = new Car( );
// Invoke methods on objects car1.changeSpeed(50); car1.speedUp(10); car1.changeGear(2); car1.printStates( );
car2.changeSpeed(60); car2.speedUp(10); car2.changeGear(3); car2.speedUp(10); car2.changeGear(4); car2.applyBrakes(15); car2.printStates( ); } }
The output of this test program will be:
Speed:60 Gear:2 Power: Speed:65 Gear:4 Power:
What Is Inheritance? Different kinds of objects often have a certain amount in common with each other. Sports Car, Luxury Car, and Common Car, for example, all share the characteristics of Cars (current speed, current gear, current power). Yet each also defines additional features that make them different: Sports car often have 2 seats and more gears; Luxury car have bigger size and more capacity; Common cars have often compact size and more capacity and sometimes low power. Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Car now becomes the superclass of sportsCar, LuxuryCar, and CommonCar. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses.
A hierarchy of Car Classes
The syntax for creating a subclass is simple. At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from:
class SportsCar extends Car { // new fields and methods defining a SportsCar would go here }
This gives SportsCar all the same fields and methods as Car, yet allows its code to focus exclusively on the features that make it unique. This makes code for your subclasses easy to read. However, you must take care to properly document the state and behavior that each superclass defines, since that code will not appear in the source file of each subclass.
Car
SportsCar LuxuryCar CommonCar
void speedUp(int increment) { speed = speed + increment; power = speed * 10; }
void applyBrakes(int decrement) { speed = speed - decrement; power = speed * 10; }
void printStates( ) { System.out.println("Speed:" + speed + " Gear:" + gear + " Power:" + power); } }
Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
What Is a Package? A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. 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. The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library is known as the "Application Programming Interface" or "API" for short. Its packages represent the tasks most commonly associated with general-purpose programming. For example, a String object contains state and behavior for character strings; a File object allows a programmer to easily create, delete, inspect, compare, or modify a file on the filesystem; a Socket object allows for the creation and use of network sockets; various GUI objects control buttons and checkboxes and anything else related to graphical user interfaces. There are literally thousands of classes to choose from. This allows you, the programmer, to focus on the design of your particular application, rather than the infrastructure required to make it work.