Objects - You can say that Objects are representations of physical elements. For eg. Bicycle. A complete status or Objects will have properties and states. This is similar to objects in pl/sql; in pl/sql we define the objects in terms of the attributes or columns.
Bicycle states (current speed, current pedal cadence, and current gear).
Methods - Method is the process by which the states are changed. This is analogous to a procedure in pl/sql.
Classes - You can think of class as a collection of methods and object properties.
For eg. In the below Bicycle class pedal cadence, speed and gear are the properties/states.
changeCadence, changeGear, etc are methods to bring about changes in the
states or properties.
class Bicycle { int cadence = 0; int speed = 0; int gear = 1; 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); } }
Inheritance - In reality there are many kinds of bikes like mountain bikes, dirt bikes, etc ... Each of the above can be classified as a class in its own due to some unique features while some features would be same in all the bikes. Say the properties of the class Bicycle would apply to both MountainBikes and DirtBikes hence you would like to extend the properties and methods of class Bicycle to Classed MountainBikes and DirtBikes - this lending of a properties and methods of a class to another is called inheritance.
Syntax ...
class MountainBike extends Bicycle { // new fields and methods defining a mountain bike would go here
}
Interfaces - collection of methods without bodies. In analogy with pl/sql you can say its a package specification.
To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such asinterface Bicycle { void changeCadence(int newValue); // wheel revolutions per minute void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); }
ACMEBicycle
), and you'd use the implements
keyword in the class declaration: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.class ACMEBicycle implements Bicycle { // remainder of this class implemented as before }
Packages - A package is a namespace that organizes a set of related classes and interfaces.
No comments:
Post a Comment