Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC124 Assignment 4 on Inheritance due next Friday.

Similar presentations


Presentation on theme: "CISC124 Assignment 4 on Inheritance due next Friday."— Presentation transcript:

1 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

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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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: Fall 2018 CISC124 - Prof. McLeod

22 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

23 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

24 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

25 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

26 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

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


Download ppt "CISC124 Assignment 4 on Inheritance due next Friday."

Similar presentations


Ads by Google