311 likes | 519 Views
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
E N D
Object-Oriented Programming(mostly in Java) CSCI 3333 Data Structures byDr. Bun YueProfessor of Computer Scienceyue@uhcl.eduhttp://sce.uhcl.edu/yue/2013
Acknowledgement • Mr. Charles Moen
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)
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
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.
Java’s Package • Comparable to C++’s namespace. • Use for disambiguation of names. • Increase reusability.
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
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; } }
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
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
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.
Compilation and Execution javac edu\uhcl\sce\yue\simple\HelloWorld.java java edu.uhcl.sce.yue.simple.HelloWorld
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
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.
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).
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.
OOP Tips • Always think about the right access level when defining data members and methods.
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.
Abstract Classes • An abstract class • Can be sub-classed • Cannot be instantiated.
OOP Tips • Think about the appropriate constructs to be used: • Class • Abstract Class • Sub-class • Interface • Observe how other people do so.
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.
Pure Virtual Methods • Those methods that must be defined by the sub-classes. • Java: use the keyword abstract. • C++: use the syntax =0;
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)
Example • Interfaces are implemented by classes. • Variable can be declared as an interface.
Java’s interface • Support interface inheritance • Support interface multiple inheritance
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 { }
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."); }
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(); } };
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.