Basic Object-Oriented
Programming in Java
Originals of Slides and Source Code for Examples:
http://courses.coreservlets.com/Course-Materials/java.html
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, Servlets, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
2 Developed and taught by well-known author and developer. At public venues or onsite at your location.
© 2012 Marty Hall
For live Java EE training, please see training courses
at http://courses.coreservlets.com/.
JSF 2.0, PrimeFaces, Servlets, JSP, Ajax (with jQuery), GWT,
Android development, Java 6 and 7 programming,
SOAP-based and RESTful Web Services, Spring, Hibernate/JPA,
XML, Hadoop, and customized combinations of topics.
Taught by the author of Core Servlets and JSP, More
Servlets and JSP, and this tutorial. Available at public
venues,Customized
or customized versions
Java EE Training: can be held on-site at your
http://courses.coreservlets.com/
organization. Contact for details.
Java, JSF 2, PrimeFaces, Servlets, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
, Topics in This Section
• Similarities and differences between Java
and C++
• Object-oriented nomenclature and
conventions
• Instance variables (fields)
• Methods (member functions)
• Constructors
• Example with four variations
“Object-oriented programming is an exceptionally bad idea which could only have
originated in California.” -- Edsger Dijkstra, 1972 Turing Award winner.
4
© 2012 Marty Hall
Basics
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, Servlets, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
5 Developed and taught by well-known author and developer. At public venues or onsite at your location.
, Object-Oriented Programming in
Java
• Similarities with C++
– User-defined classes can be used like built-in types.
– Basic syntax
• Differences from C++
– Methods (member functions) are the only function type
– Object is the topmost ancestor for all classes
– All methods use the run-time, not compile-time, types (i.e. all Java
methods are like C++ virtual functions)
– The types of all objects are known at run-time
– All objects are allocated on the heap (always safe to return objects
from methods)
– Single inheritance only
• Comparisons to C#
– C# very similar to Java in OOP. For details, see
6
http://www.harding.edu/fmccown/java1_5_csharp_comparison.html
Object-Oriented Nomenclature
• “Class” means a category of things
– A class name can be used in Java as the type of a field or
local variable or as the return type of a function (method)
• “Object” means a particular item that
belongs to a class
– Also called an “instance”
• Example
String s1 = "Hello";
– Here, String is the class, and the variable s1 and the value
"Hello" are objects (or “instances of the String class”)
7