160 likes | 179 Views
Learn about Object-Oriented Programming (OOP) principles in Java, including classes, objects, inheritance, interfaces, and packages. Explore the benefits of modularity, information-hiding, code re-use, and easy debugging in OOP. Understand user-defined classes, constructors, methods, and fields for effective Java programming.
E N D
By : Robert Apeldorn An Introduction to Java – Part II Basic OOP
What is OOP? • Object-oriented programming is a programming paradigm that uses “objects” to design applications and computer programs
Terms needed to know • Class • blueprint or prototype from which objects are created • Object • data structures consisting of data fields and methods together with their interactions • Inheritance • "Subclasses" are more specialized versions of a class, which inherit attributes and behaviors from their parent classes, and can introduce their own. • Interface • group of related methods with empty bodies • Package • namespace that organizes a set of related classes and interfaces
Objects • An object stores its state in fields and exposes its behavior through methods • Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication
Pros with Objects • Modularity • The source code for an object can be written and maintained independently of the source code for other objects • Information-hiding • By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world • Code re-use • If an object already exists, you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code • Pluggability and debugging ease • If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement
Classes • The basic structure of Java programs with user defined classes: public class ClassName { public static void main(String[] args) { program statements } user defined methods } user defined classes
Class Definitions • Constructors • special block of statements called when an object is created, either when it is declared or when it is dynamically constructed on the heap through the keyword “new” • Methods • consists of a sequence of programming statements to perform an action • Field • data member of the class
Different Class Modifiers • Public • Can be accessed outside the program • Private • Cannot be accessed outside of the current class • Protected • A subclass can directly access a superclass • Ex) subclass square can access class shape
Example of a Class 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); } }
How to use inheritance Class MountainBike extends Bicycle { //properties of mountain bike }
Interface example public interface Bicycle { void changeCadence(int newValue); // wheel revolutions per minute void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); }
How to implement the interface Class ACMEBicycle implements Bicycle { // remainder of this class implemented as before }
Sources • “Java OOP basics” • http://www.cs.drexel.edu/~knowak/cs265_fall_2009/java_OOP_and_%20inheritance.pdf • The Java Tutorials. Trail: Learning the Java Language. Oct 19, 2009. <http://java.sun.com/docs/books/tutorial/java/index.html>