CISC124 Assignment 4 on Inheritance due next Friday.

Slides:



Advertisements
Similar presentations
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Advertisements

ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
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
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Notices Assn 2 is due Friday, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including Thursday’s lecture: Big Topics.
Lecture 5:Interfaces and Abstract Classes
Inheritance and Polymorphism
Project Topic 2: Migration to Java 9
Fall 2017 CISC124 9/18/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CMPE212 – Stuff… Assn 2 due next Friday. Winter 2018
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Chapter 6 Methods: A Deeper Look
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Last Quiz this week. Topics listed in last Tuesday’s lecture.
CISC124 Quiz 1 marking nears completion!
MSIS 655 Advanced Business Applications Programming
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
Interfaces.
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
Advanced Java Programming
CISC124 Assignment 4 on Inheritance due today at 7pm.
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC124 Assignment 3 due tomorrow at 7pm.
Java Inheritance.
Fall 2018 CISC124 2/1/2019 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 sample solution is posted.
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
CMPE212 – Reminders Assignment 3 due next Friday.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CMPE212 – Reminders Course Web Site:
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
Chapter 8 Inheritance Part 2.
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 due in a week.
CMPE212 – Reminders Assignment 2 due today, 7pm.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Presentation transcript:

CISC124 Assignment 4 on Inheritance due next Friday. Fall 2018 CISC124 1/18/2019 CISC124 Assignment 4 on Inheritance due next Friday. Quiz 2 this week in the lab. Topics in Oct. 22 lecture notes. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

Today Continue Interfaces. Modules – first used in Java 9. Fall 2018 CISC124 - Prof. McLeod

Interfaces in Java 9, Cont. To summarize, an interface can now contain: public static final attributes. abstract methods. default methods. static methods. private methods. private static methods. The keywords public and abstract are still not required. What is not allowed? Fall 2018 CISC124 - Prof. McLeod

Interfaces in Java 9, Cont. So, now an interface does not have to be just a design specification. It can contribute to and simplify the construction of an object hierarchy. Fall 2018 CISC124 - Prof. McLeod

public class Test implements interface1, interface2, interface3, … {} Interfaces, Cont. Interfaces do not extend Object. Interfaces can extend multiple interfaces (but not other classes). Classes can implement one or many interfaces: public class Test implements interface1, interface2, interface3, … {} If two interfaces have the same method name, the implementing class must implement that method, even if it is a default method. Fall 2018 CISC124 - Prof. McLeod

Interfaces, Cont. An interface cannot be instantiated, but you can use the interface type as if it is a class. The interface acts as a “stand-in” for the concrete object that will replace it when the program is running. The interface “guarantees” the required behaviour of the object it is standing in for. Fall 2018 CISC124 - Prof. McLeod

Interfaces, Cont. Attributes in interfaces can only be public final static, you cannot use private. You don’t even have to specify public final static as it is assumed. Constants must be initialized. In Java 8 and 9, you should not and sometimes cannot use the public and abstract keywords in method headers. They are not needed anyways… Fall 2018 CISC124 - Prof. McLeod

Interfaces, Cont. A class that implements an interface must have a concrete implementation of every abstract method signature in the interface. The class can just accept (inherit) default methods, can use static methods and will not see private methods. When designing an interface, you must try to use unique method and constant names, in case your interface becomes part of a multiple implementation. Fall 2018 CISC124 - Prof. McLeod

The Comparable Interface in Java Without all the javadoc comments: package java.lang; public interface Comparable<T> { public int compareTo(T o); } That’s it!! This is a Generic Interface now. Fall 2018 CISC124 - Prof. McLeod

Comparable Interface, Cont. A class that implements Comparable can be sorted with: Arrays.sort(), or Collections.sort(), or from ArrayList.sort() The first two methods are already contained in Java (package java.util) and use very fast Mergesort or Quicksort algorithms. Why is this implementation necessary? Fall 2018 CISC124 - Prof. McLeod

Implementation in Arrays.mergesort() For example, this is a line of code from the mergeSort() method in the java.util.Arrays class: if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) This method sorts src[], which is of type: Object[]. So, provided your Object implements Comparable, this method can sort it! Fall 2018 CISC124 - Prof. McLeod

Interfaces, Cont. While interfaces are not proper objects in Java, you can “pretend” that they are in some circumstances. For example, a class can use the interface as an object type, and write code as if you are invoking a method declared in the interface. You can pretend that the specification is an object. Part of polymorphism. An instance of a class that implements an interface can be cast to that interface type. Fall 2018 CISC124 - Prof. McLeod

Another Example on Interfaces Look at DoRunRunDemo and see how it works. The example contains examples of how the interface can (and cannot) be used in Java 8 and 9. Fall 2018 CISC124 - Prof. McLeod

Summary of Interface Demo Inside the interface: Can only have public final static attributes. Can have public abstract, public static, public default, private and private static methods. Do not use the public or abstract access modifiers. Public static methods can invoke private static methods. Public default methods can invoke private non-static methods. Fall 2018 CISC124 - Prof. McLeod

Summary of Interface Demo, Cont. Inside a class implementing the interface: Must implement all abstract methods (or this class will be abstract). Constants are inherited. Default methods can be inherited, made abstract (in which case the class will be abstract) or overridden. Public static methods can be invoked from the interface directly. Fall 2018 CISC124 - Prof. McLeod

Summary of Interface Demo, Cont. Inside some other class: Constants and static methods can be accessed directly through the interface. The interface cannot be instantiated. A class implementing the interface can be instantiated. Pointers can be declared to be of the interface type and they can then point to instances of a class that implements the interface. A class implementing the interface can be cast to be of the interface type. Fall 2018 CISC124 - Prof. McLeod

Java Modules This new system of organizing large numbers of Java source code files was introduced in Java 9. Modules are a way to organize many packages into a single module. But there is a little more to it than just grouping packages and files together. Each module must have a module-info.java file: Fall 2018 CISC124 - Prof. McLeod

module-info.java The module can be built with a large set of packages in a folder structure. The module-info file goes in the root folder of this structure. This file contains information about the name of the module, the packages that are available, and the packages that are required. Basically, the info file supplies all the needed information about how the module interacts with other modules. Fall 2018 CISC124 - Prof. McLeod

module-info.java, Cont. The format of the file is: module module_name { requires module_name; … exports package_name; } You can have as many requires and exports lines as needed. Fall 2018 CISC124 - Prof. McLeod

module-info.java, Cont. requires identifies modules on which this module depends. exports identifies packages in the current module that are available to other modules. This provides encapsulation at the package level! uses identifies specific interfaces and abstract classes or “services” that are used by this module. Fall 2018 CISC124 - Prof. McLeod

open, opens, provides, with, to, transitive module-info.java, Cont. Other keywords that can be used in the info file: open, opens, provides, with, to, transitive See help docs such as the API docs for the java.lang.module package. Or: https://www.oracle.com/corporate/features/understanding-java-9-modules.html Fall 2018 CISC124 - Prof. McLeod

Java Modules, Cont. You can also restrict what other modules can see using Reflection. (More on reflection later…) Dependencies can be specified for use at compilation or at run time or both. Encapsulation is a big advantage. The specification of dependencies reduces the run time size of the program and will speed up execution as a result. A module name must be unique. Fall 2018 CISC124 - Prof. McLeod

Java Modules, Cont. In this course we won’t need to create modules for our little projects… The Java platform itself is now organized into 95 (or more) modules. Each module can contain many packages. A lot of the stuff we have been using is in the java.base module. Fall 2018 CISC124 - Prof. McLeod

Java Libraries A library is distributed and used from a *.jar file (“Java Archive”) on the classpath. It is a compressed file containing only *.class bytecode files organized into the package folder structure specified by the packages. Now, *.jar files also contain the required module-info.class files called “module descriptors”. The *.jar file does not contain any *.java files – these will be in a file called “src.zip” that only comes with the JDK: Fall 2018 CISC124 - Prof. McLeod

Aside – Viewing Java API Source Code Open up the library link. Most of what we are interested in, for now, will be in the java.base module. When you locate the class or interface of interest and are doing this for the first time, you will see: Fall 2018 CISC124 - Prof. McLeod

Aside – Viewing Java API Source Code, Cont. Click on “Attach Source…” Choose “External location”, and click on “External File”. Fill out the dialog by providing the location of the src.zip file for the JDK you are using: Fall 2018 CISC124 - Prof. McLeod

Aside – Viewing Java API Source Code, Cont. Let’s look something up! Fall 2018 CISC124 - Prof. McLeod