1 / 15

An Introduction to Java – Part II Basic OOP

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

jnewton
Download Presentation

An Introduction to Java – Part II Basic OOP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. By : Robert Apeldorn An Introduction to Java – Part II Basic OOP

  2. What is OOP? • Object-oriented programming is a programming paradigm that uses “objects” to design applications and computer programs

  3. 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

  4. 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

  5. Example of a bicycle

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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); } }

  11. Inheritance

  12. How to use inheritance Class MountainBike extends Bicycle { //properties of mountain bike }

  13. 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); }

  14. How to implement the interface Class ACMEBicycle implements Bicycle { // remainder of this class implemented as before }

  15. 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>

More Related