Lecture 30 Revision Lecture 1

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
ITEC200 – Week03 Inheritance and Class Hierarchies.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
JAVA BASICS Prepared by The Smartpath Information Systems
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Chapter 11 Inheritance and Polymorphism
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Java Programming Language
Lecture 27 Creating Custom GUIs
CompSci 230 S Programming Techniques
Inheritance in Java.
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Comp 249 Programming Methodology
Functional Programming with Java
Object Oriented Programming
Chapter 9 Inheritance and Polymorphism
Java Programming Language
Polymorphism and access control
Polymorphism, Abstract Classes & Interfaces
CSE 1030: Implementing GUI Mark Shtern.
Java – Inheritance.
Java Inheritance.
Fundaments of Game Design
Review of Previous Lesson
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
C++ Object Oriented 1.
Presentation transcript:

Lecture 30 Revision Lecture 1

Summary of Previous Lecture Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Interrupting Threads InterruptedException Handling Exceptions Waiting for thread execution awaitTermination() Thread.join() Returning a result from asynchronous threads Callable interface Retrieving the result Future Interface FutureTask implementation

Exporting your Assignment Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary The purpose of using Eclipse is to make it standard for markers to import, view your source code and run your simulation. If you have used a different IDE, please create an Eclipse project and copy your files in to setup a project similar to the one above. You can then follow our steps to export

Exam Format/Topics Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary 51 Multi-choice Questions. Total marks 60. Questions will be worth either 1 mark or 1.5 marks. Topics : Angela Access Modifiers (public, private, default, package) Overriding/Overloading methods Constructors Class inheritance Nested, Anonymous, Abstract Classes Fields (Instance/Static/Member Class) static keyword final keyword Topics : Craig/Elliot Using Swing Components JFrame, JPanel, JComponent Events/Event Listeners Adapters

Exam Format/Topics GUI Thread/Event Dispatch Thread (EDT) Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary GUI Thread/Event Dispatch Thread (EDT) SwingUtility/SwingWorker classes Benefits of Model-View-Controller (MVC) pattern Regex! File IO Classes and methods Binary (De)Serialization of Objects Java Threads (Under the hood) States Runnable() Interface ExecutorService Class / Methods Java Monitor/Monitor Locks Daemon Threads Producer/Consumer Relationship ArrayBlockingQueue Future/Callable interfaces

Object Oriented Programming Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Class : A blueprint from which objects are created Explains state (fields) and behavior (methods) Data encapsulation Often models real-world objects Object : Instance of a Class that is created at run-time Has its own state separate from other instances Declared with a type Initialized with a constructor Type Name Constructor Method

Object Oriented Programming Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Inheritance : Mechanism for organizing and structuring software Classes can inherit fields and methods from superclass Extends Implements Is-A relationship e.g. Bicycle is a Vehicle Interface : Contract between a class and its functionality If a class implements an interface, it must provide functionality for all the defined methods Ensures all classes implementing the interface will provide common functionality and can be used similarly.

Java Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Java : Source code is written in text files ending with the .java extension. These source files are compiled into .class files. Java Virtual Machine (JVM) : .class files contain bytecode that are interpreted by the JVM, which executes the code on the machine. Creates a layer between processor and executing code. Errors: Syntax Errors : Errors are found at compile-time where the compiler will find an error preventing the compilation of class files Runtime Errors : Program was compiled successfully, however an error was thrown during execution Logic Errors : No explicit errors are thrown but has unexpected output or results.

Java Static typed language Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Static typed language Variables must be declared before that can be used. Bicycle bike; Variables can then be initialized to receive a value of same type bike = new Bicycle(); final keyword Indicates that a variable’s value cannot be changed once initialized When a list is declared as final, the objects within the list can still change their value. Objects can also be added and removed from the list. However, a new List instance cannot be assigned to the final variable

Expressions - Operators Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary

Conditionals If, else, else if statements Switch statements For Loops Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary If, else, else if statements Switch statements For Loops Enchanced For Loops While Loops Do While Loops Ternary Operations

Methods Static Methods e.g. result = Math.ceil(); Calling local methods e.g. returnVar = methodName(parameters) Invoking methods on objects e.g. object.methodName(parameters); Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary The method definition will contain modifiers (e.g. visibility, static, final etc.), method name, return type and parameters (with types!) Method Call Stack

Methods Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Overloading : Declaring methods of the same name with different amounts of parameters. Usually used to perform similar tasks with different types/amounts of arguments. Compiler recognizes this by the method signature, e.g. name/parameters – not return type

Arrays Arrays Declaring an array Initializing an array – Fixed size Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Arrays Declaring an array Initializing an array – Fixed size Adding values or objects to an array e.g. courseMarks = new int[]{26,73,55,97}; ArrayIndexOutOfBounds exception Using the logical operators defined earlier Multi-dimensional arrays – representing grids. Cloning Arrays ArrayLists Stores Objects, not primitive values Dynamically sizing Generics – ArrayList<T>

Classes Defining a class : Class declaration and class body Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Defining a class : Class declaration and class body Field and Method declarations/logic Instance variables : non-static fields Class variables : static fields Visibility Modifiers Determine the visibility of a class/variable/method/constructor public : Can be accessed from anywhere – no safety protected : Accessed from within the package No modifier/default : Accessed within the class/package private : Accessed only from within the class

Inheritance Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Is-A relationship represented by arrows. Subclass inherits non-private methods of the superclass to use. The subclass also inherits the non-private fields of the superclass. Overriding : Change the meaning (override) of the method declared in the superclass. Could either be a completely new implementation or add more functionality after call the super() method. Must override the same method signature. Cannot override final method.

Inheritance Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary The subclass can call the method of the superclass even if it doesn’t implement the method itself. We will see “SuperMethod” be printed.

Inheritance Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary If the subclass declares the superMethod() signature in its own class body, then it will override the functionality. Now when we create our subclass instance and call superMethod(), we will see the subclass implementation of that method be executed.

Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Polymorphism Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Polymorphism is the ability of objects belonging to different types to respond to method calls of methods of the same name, each one according to an appropriate type-specific behaviour. The program does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called late binding or dynamic binding). This means the compiler doesn’t necessarily know the instance type of the object that the method is being called on. The instance can be determined at run time and the same method call will have different behaviour depending on the instance type.

Polymorphism Binding Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Polymorphism Binding Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary

Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Polymorphism Binding Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Although the compiler sees each ‘cs’ variable as type CustomSuper (and hence we can only call CustomSuper methods), the instances we created were of type CustomSub and CustomOtherSub. We therefore see the overriden versions of superMethod(). Prints : “SubMethod”, “OtherSubMethod”

Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Abstract Classes Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Similar to Interfaces, Abstract classes cannot be instantiated and are instead used to define part of an implementation leaving the subclasses to implement the logic. Declared with the keyword abstract. Will often contain abstract methods, which are a method signature with no code block defining the implementation of the method. These must be implemented by all subclasses extending the abstract class. An Abstract class can have state. It may declare and initialize instance variables. An interface cannot do this.

Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Anonymous Classes Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary An Anonymous class is a local class that is not given a name but is instead declared implicitly by creating a variable of it. Because of this we can only use an anonymous class once, when is is instantiated. These should only be used when you need to use the class only once. We are combining the instantiation and defining of a class in one statement. We have seen many examples of these when developing GUIs. They are often used when creating AWT or Swing based programs. public void sayBye() { HelloWorld i = new HelloWorld() { String name = "world"; public void greet() { System.out.println("Bye " + name); } }; i.greet();

Exceptions Java uses Exceptions for error handling. Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Exceptions Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Java uses Exceptions for error handling. These must be caught using a try-catch statement, or thrown to the calling class to handle the exception (Exception Propagation). If the exception is never handled by the developer, it will be thrown to the default exception handler which will terminate the thread and display the error message. try { value = 0; int answer = 100 / value; System.out.println("This will not be printed"); //1 } catch (Exception e) { System.out.println("Error"); //2 value = -1; } System.out.println("Program can continue to run..."); //3

Exceptions Common Exceptions we dealt with : NullPointerException Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Exceptions Interrupting Threads Waiting for Threads Callable Interface Future Interface Summary Common Exceptions we dealt with : NullPointerException When using objects to execute tasks ArrayIndexOutOfBoundsException When accessing arrays ClassCastException When casting the class of an instance InterruptedException When dealing with Threads IOException Dealing with Input/Output (like files)