Programming in Java CSE301 Half Lecture Harry Erwin, PhD University of Sunderland.

Slides:



Advertisements
Similar presentations
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Advertisements

Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Inheritance Inheritance Reserved word protected Reserved word super
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
UML – Class Diagrams.
1 CS1001 Lecture Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Basic Object Oriented Concepts Overview l What is Object-Orientation about? l What is an Object? l What is a Class? l Constructing Objects from Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Objects and Classes. 3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Object Oriented Software Development
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
The Java Programming Language
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.
Inheritance Building one object from another. Background Object-oriented programming is normally described has offering three capabilities Encapsulation:
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism.
Parts of JAVA 1www.gowreeswar.com. Features of JAVA 2www.gowreeswar.com.
1 JAVA API & Packages Starring: Java Documentation Co-Starring: BlueJ IDE.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Inheritance A Review of Objects, Classes, and Subclasses.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming1 Programming.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Java 5 Part 2 CSE301 University of Sunderland Harry Erwin, PhD.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
OOP Basics Classes & Methods (c) IDMS/SQL News
Object Oriented Programming Object and Classes Lecture 3 MBY.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Some Eclipse shortcuts
Final and Abstract Classes
Packages and Interfaces
Object Oriented Programming in java
Constructors, GUI’s(Using Swing) and ActionListner
Abstract Classes and Interfaces
Final and Abstract Classes
Chap 1. Getting Started Objectives
Interfaces,Packages and Threads
Presentation transcript:

Programming in Java CSE301 Half Lecture Harry Erwin, PhD University of Sunderland

Object-Orientation and Complexity As an experienced software architect (who could earn £90,000+ per year if I were willing to die before I am 60), I will give you my perspective on object-orientation. No person can understand the simultaneous interactions involving even a small number of objects. For example, 16 objects have 240 potential interfaces, if all are active. Balancing that many interactions is beyond your skill.

Managing Complexity The novice programmer will often generate enormous class and object diagrams with potentially hundreds of interactions. Don’t go there. Use packages (subsystems) rather than classes in your top-level diagram. Between 5 and 15 subsystems and a few more interfaces. Then drill down to produce class/object/package diagrams at the next level. Iterate this process until you can define the major classes. Document class interfaces and inheritance on separate UML diagrams.

Complexity and Encapsulation Finding a system design is not a simple task. This year, we will learn a number of principles you can use. Encapsulation is the first such principle. Strive for a design that consists of tightly bound subsystems that connect via a small number of narrow interfaces—APIs. GUI design is not a good model for this, so forget what you’ve learned about using visual tools until you understand how your design choices affect the way your GUI interfaces to your software.

Using Packages To specify the package a class belongs to, use the ‘package’ keyword. Note that classes belong to an unnamed package by default. This can be very convenient, but is also bad form. Eclipse will help you here. To load a class or interface Name from a package, P, use the following directive: –import P.Name; Note that the standard java class libraries live in a number of packages like: –java.lang –java.io –java.util –javax.swing

Writing a Java File A Java file consists of: –An optional ‘package’ directive –Zero or more ‘import’ directives –One or more class or interface definitions. (At most one is allowed to be ‘public’.) –Interspersed with comments When ClassName is a public class defined in the file, the name of the file must be ClassName.java. Eclipse will help you here.

Defining a Java Program A Java program has to have a class with a method with the following signature: –‘public static void main(String[] args);’ Assuming a public class exists with class name (e.g., CN) containing a method with the appropriate signature, it can be run by the following command: –‘java CN [list of arguments]’. Eclipse allows you to set the arguments as run properties.

Using Libraries Java has thousands of utility classes organized into libraries. We will be using some of them, for example: –java.io.* for input/output –java.lang.* for standard features and threads –java.text.* for string processing –java.util.* for utilities and collection classes –java.awt.* for the abstract windowing toolkit –javax.swing.* for the swing classes. –etc…

Class or Instance Methods? Instance methods require an instance. If you find yourself frequently creating instances as temporary variables for use with some method, you should define a class (static) method to do the same thing. Similarly, you might find class methods useful for doing comparisons among individual instances, since the class method can access the private fields of the instances passed to it as arguments.

Chaining Constructors It’s often convenient to chain constructors, calling one from another using the ‘this(argument list);’ syntax. This is especially frequent in the input/output library. The BankingDataReader gives some good examples of this.

Memory Leaks Memory leaks can occur in Java if a valid but unused reference to an object is left in scope: –int big_array[] = new int[100000]; –int result = foo(big_array) –// big_array is no longer needed but remains in –// scope. –big_array = null; // dumps it to avoid a leak –for(;;)handle_input(); // loops forever Hash tables can also be a source of memory leaks.

Interfaces Interface Dig_up { Object find(String name); } Any class with a find function of the correct signature can claim to implement the Dig_up interface. Note that you can do this in C++ as well, but it’s much rarer there.

Constructors Assume Foo is a class with a default constructor (Foo()) and an int member field, val. The following is good Java coding style for a second constructor: Foo(int val) { this(); // default constructor this.val = val; // Works! // sets the member field to val }

Role of ‘this’ 1.Used as a reference to the current object, much as in C++. 2.this.bar is a reference to the bar field of the current object. Ditto for this.bar() 3.this() is used in initialization, much like super().