1 / 31

Object-Oriented Programming (mostly in Java)

Object-Oriented Programming (mostly in Java). CSCI 3333 Data Structures. by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu http://sce.uhcl.edu/yue/ 2013. Acknowledgement. Mr. Charles Moen. Using Java at UHCL. Java Versions and Compilers. Latest: Sun’s Java SE7

jory
Download Presentation

Object-Oriented Programming (mostly in Java)

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. Object-Oriented Programming(mostly in Java) CSCI 3333 Data Structures byDr. Bun YueProfessor of Computer Scienceyue@uhcl.eduhttp://sce.uhcl.edu/yue/2013

  2. Acknowledgement • Mr. Charles Moen

  3. Using Java at UHCL Java Versions and Compilers • Latest: Sun’s Java SE7 • All homework needs to be compiled on Oralce/Sun’s version of javac • NetBeans: free from Sun • Eclipse: open source Java’s IDE, will be used here. • Available at: • UHCL Delta Building PC Lab (Delta Bldg., second floor)‏ • Delta 118 (Windows laboratory)‏

  4. Brief Facts About Java (Deitel)‏ Brief Facts About Java • Created by James Gosling at Sun and released in 1995 • Very object-oriented • Quite platform independent • Java Collections Framework • Included with the JDK • Ready made classes for data structures Dr. James Gosling From eWeek.com, April 14, 2006

  5. Some Main Java Tools • javac: compiler to bytecode • .java -> .class • java: Java Virtual Machine (JVM) • Execute Java class files. • javadoc: generate Java documentation in HTML. • jar: Java Archive for bundling files for deployment.

  6. Java’s Package • Comparable to C++’s namespace. • Use for disambiguation of names. • Increase reusability.

  7. Brief Facts About Java (Goodrich, 46)‏ Java’s Package • The “package” statement defines a series of nested folders where your program files are located • Helps keep the files organized • Goes on the first non-comment, non-space line in the java file • By convention, to guarantee uniqueness, use a reversed domain name, followed by a unique word, all lower case • If used, place all your code for a homework assignment in the same package, so they’ll be in the same directory. package edu.uhcl.sce.hello; Package Goes on the first non-comment line The package matches the nested subdirectories where your code is stored

  8. Simple Java’s Class /** * Simple Java Class (Not a program) */ package edu.uhcl.sce.yue.simple; public class Greeting { private String greeting; public Greeting(String greeting){ this.greeting = greeting; } public String getGreeting(){return greeting;} public void setGreeting(String greeting){ this.greeting = greeting; } }

  9. Compilation • Compilation and execution of Java programs with package should be in the parent directory of the package. E.g. javac edu\uhcl\sce\yue\simple\Greeting.java

  10. Simple Java’s Class Comment /** * Simple Java Class (Not a program) */ package edu.uhcl.sce.yue.simple; public class Greeting { private String greeting; public Greeting(String greeting){ this.greeting = greeting; } public String getGreeting(){return greeting;} public void setGreeting(String greeting){ this.greeting = greeting; } } Package statement Class name Instance variable Constructor initializes the instance variables Public method to get the data Public method to change the data

  11. A Simple Java Program All java code must be within a class Curly braces around the class body /** * Simple Java Example. */ package edu.uhcl.sce.yue.simple; public class HelloWorld { public static void main (String args[]) { Greeting greeting = new Greeting("Hello, World"); System.out.println(greeting.getGreeting()); } } “Main” method “new” calls the constructor All Java statements end with a semicolon Package belonging to.

  12. Compilation and Execution javac edu\uhcl\sce\yue\simple\HelloWorld.java java edu.uhcl.sce.yue.simple.HelloWorld

  13. Some common OO Concepts • Object identity: important in database. • Class: abstract data type and encapsulation: an object is encapsulated by its methods. • Inheritance: superclass/subclass

  14. Some common OO Concepts • Dynamic binding: mapping of method to code at runtime (e.g. Java’s interface and polymorphism) • Subtype polymorphism: redefinition of methods at subclass and dynamic binding of method.

  15. OOP in Java • ADT: Class • Data: usually private • Methods: usually: • public: defining how the class should be used. • protected: mainly for sub-classing. • private: helper methods. • default (no modifier): helper methods for package • Can be instantiated (created an object).

  16. Access Level in Java

  17. Three Access Levels in C++ • private: accessible within the class and by friends. • C++ allows friend classes and methods! • protected: accessible by the class and subclasses • public: accessible by all.

  18. OOP Tips • Always think about the right access level when defining data members and methods.

  19. Inheritance • There are two kind of inheritance in Java: • Sub-classes • Sub-interfaces • C++: • Does not support Java’s style interface. • Support multiple class inheritance. • Java: • Support multiple interface inheritance. • Does not support multiple class inheritance.

  20. Abstract Classes • An abstract class • Can be sub-classed • Cannot be instantiated.

  21. OOP Tips • Think about the appropriate constructs to be used: • Class • Abstract Class • Sub-class • Interface • Observe how other people do so.

  22. Virtual Methods • Virtual methods: those that can be redefined (extended) by subclasses. • Java’s methods: virtual by default. • Java: use the keyword final for non-virtual methods. • C++’s methods: use the keyword virtual. • See the example in Wikipedia.

  23. Pure Virtual Methods • Those methods that must be defined by the sub-classes. • Java: use the keyword abstract. • C++: use the syntax =0;

  24. Java’s Interface • “Contract-based” programming. • Incorporate ADT ideas. • An interface specifies what methods a class implementing the interface must support. • An interface may have • Method signatures (but not body) • Constants (but not data member)

  25. Example • Interfaces are implemented by classes. • Variable can be declared as an interface.

  26. Java’s interface • Support interface inheritance • Support interface multiple inheritance

  27. Example package edu.uhcl.sce.yue.simple; interface WoodThingy { public String getColor(); public void setColor(String color); } interface Ducky { public void quack(); public void fly(); } interface WoodDucky extends WoodThingy, Ducky { }

  28. Example public class MyWoodenDuck implements WoodDucky { String color = "green"; public MyWoodenDuck() {} public String getColor() { return this.color; } public void setColor(String color) { this.color = color; } public void quack() { System.out.println("quack, quack."); public void fly() { System.out.println("fly, fly."); }

  29. Example public static void main(String[] args) { MyWoodenDuck wd = new MyWoodenDuck(); wd.setColor("yellow"); System.out.println("Duck color: " + wd.getColor()); wd.quack(); wd.fly(); WoodDucky wdi = new MyWoodenDuck(); wdi.setColor("red"); System.out.println("Duck color: " + wdi.getColor()); wdi.quack(); wdi.fly(); } };

  30. Use of Java’s interface • Support ADT, including information hiding,. • Support ‘Contract-based’ programming. • Allow specification of common methods for multiple classes within using class inheritance.

  31. Questions and Comments?

More Related