CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Programming in Java CSCI-2220 Object Oriented Programming.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
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.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
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.
Today Enumerated Types - Summary. Start Nested Classes: Inner Classes, Local Classes, Anonymous Classes, Lambda Functions Next: Interfaces, Abstract Classes.
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.
Inheritance and Polymorphism
Software Engineering Fall 2005
CMPE212 – Stuff… Assn 2 due next Friday. Winter 2018
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
Nested class.
CISC/CMPE320 - Prof. McLeod
Object Oriented Programming
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
Inheritance Basics Programming with Inheritance
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Interface.
Java Programming Language
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Last Quiz this week. Topics listed in last Tuesday’s lecture.
Chapter 9: Polymorphism and Inheritance
Packages and Interfaces
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.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC124 Assignment 4 on Inheritance due today at 7pm.
Polymorphism Polymorphism
Computer Programming with JAVA
Java – Inheritance.
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.
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
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.
Chapter 14 Abstract Classes and Interfaces
CMPE212 – Reminders Assignment 3 due next Friday.
Winter 2019 CMPE212 4/20/2019 CMPE212 – Reminders
CMPE212 – Reminders Course Web Site:
Review of Previous Lesson
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 due today, 7pm.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
C++ Object Oriented 1.
Presentation transcript:

CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week. Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week. JUnit testing code supplied with assignments 3 and 4 has been updated to run with JUnit 5. We have covered what you need to know for assn 3. Winter 2018 CMPE212 - Prof. McLeod Prof. Alan McLeod

Today Inner Classes. Anonymous Classes. Abstract Classes. Interfaces – in more detail (If we have time.) Winter 2018 CMPE212 - Prof. McLeod

Upcoming Topics & Terms Hierarchies, Inheritance, Polymorphism Interfaces, Anonymous Classes, Abstract Classes, Lambda Functions, Inner Classes Generic Classes Winter 2018 CMPE212 - Prof. McLeod

Inner Classes Simply defined as a class defined within a class! Neat! Winter 2018 CMPE212 Inner Classes Simply defined as a class defined within a class! Neat! public class OuterClass { private class InnerClass { Attributes of innerclass Methods of innerClass } // end InnerClass Attributes of OuterClass Methods of OuterClass } // end OuterClass Winter 2018 CMPE212 - Prof. McLeod Prof. Alan McLeod

Inner Classes, Scope Rules See the demo program InnerClassDemo.java, used with TestInnerClassDemo.java. The inner class can easily get at all the attributes and methods of the outer class. The outer class can also access the attributes and methods of the private inner class, after it instantiates the inner class. However, all private classes, methods and attributes are hidden outside InnerClassDemo. Winter 2018 CMPE212 - Prof. McLeod

Inner Classes, Why private? If you are declaring a non-static object inside your class, you are only doing so because you want to hide it away. This object can only be used inside the outer class and is hidden everywhere else. How else can you make a class private? Winter 2018 CMPE212 - Prof. McLeod

Inner Classes, Cont. So, what’s the point of private inner classes? (Used quite often with GUI coding…) One reason - When you wish to use a small class (in terms of the size of its definition), but don’t want to bother creating a separate class definition file. Winter 2018 CMPE212 - Prof. McLeod

Inner Classes, Cont. An inner class can be a “hidden” object! This is often used with Linked List definitions to define the node object. Yes, you can have an inner class inside an inner class! Fun! Winter 2018 CMPE212 - Prof. McLeod

public static Inner Classes What is the point of declaring an inner class public static? It would allow you to “categorize” methods into topical groups. Invoke them as below: First: TestClass tc = new TestClass(); Then, invoking methods from public static inner classes: tc.Group1.method(); tc.Group2.anotherMethod(); Winter 2018 CMPE212 - Prof. McLeod

Anonymous Classes Are a kind of inner class. Winter 2018 CMPE212 Anonymous Classes Are a kind of inner class. Defined by putting the code for the class right in the instantiation statement for the object! Winter 2018 CMPE212 - Prof. McLeod Prof. Alan McLeod

Anonymous Classes, Example public class AnonymousClassDemo { public static void main(String[] args) { MessageSender ms = new MessageSender() { public void sendGreeting (String name) { System.out.println("Hello " + name + "!"); } // end sendGreeting }; // end ms, NOTE ";"! ms.sendGreeting("Alan"); } // end main } // end AnonymousClassDemo Winter 2018 CMPE212 - Prof. McLeod

Anonymous Classes, Example - Cont. Displays “Hello Alan!” to the console (Wow!) MessageSender is an interface, not an Object: public interface MessageSender { void sendGreeting (String name); } // end MessageSender interface So, you have not actually named the Object that contains the definition of the method sendGreeting()! ms is an Anonymous Object. Winter 2018 CMPE212 - Prof. McLeod

Anonymous Classes, Cont. Some coders will tell you that anonymous classes are just classes written by lazy coders. This is true, but you will see these little beasties used with GUI coding… (especially by lazy GUI coders, or the GUI wizards in NetBeans and Eclipse). Java 8 now has a very tidy solution to using messy anonymous classes in a GUI program – using Lambda Functions – more later! Winter 2018 CMPE212 - Prof. McLeod

Anonymous Classes, Example - Cont. How would you do this is a “non-anonymous” way? First, create a named class that implements the interface: public class ImplementingSender implements MessageSender { public void sendGreeting(String name) { System.out.println("Hello " + name + "!"); } Winter 2018 CMPE212 - Prof. McLeod

Anonymous Classes, Example - Cont. Then, instantiate this class and invoke the method: public class TestSender { public static void main(String[] args) { ImplementingSender iAmNamed = new ImplementingSender(); iAmNamed.sendGreeting("Alan"); } Now, iAmNamed is of a named type. Winter 2018 CMPE212 - Prof. McLeod

Next… Interfaces are completely abstract – they cannot contain much in the way of concrete code. More shortly. First, look at something in-between an interface and a fully concrete class – an abstract class: Winter 2018 CMPE212 - Prof. McLeod

public abstract MyClass … Winter 2018 CMPE212 abstract Classes It is not unusual to declare a class in the root of a hierarchy to be abstract: public abstract MyClass … Any class declared this way cannot be instantiated. It can only be extended. Unlike an interface, an abstract class can also contain concrete method definitions and any kind of attribute. If a class has one or more abstract methods, the class must be declared abstract as well. Winter 2018 CMPE212 - Prof. McLeod Prof. Alan McLeod

public abstract String getListing(); abstract Classes, Cont. abstract methods have no code in them. For example: public abstract String getListing(); A class that extends an abstract class must override all the abstract methods in the class, unless it wants to be abstract too. Unlike the interface, you should write public abstract for each method signature. Winter 2018 CMPE212 - Prof. McLeod

abstract Classes, Cont. Why bother? An abstract class forces sub-classes to define certain methods. This helps ensure that the hierarchy has a consistent design. Also, when declaring a method in a very abstract class, then you don’t have to worry about what to do in the method body, especially if it must return a value. Provides the mechanism for polymorphism! Winter 2018 CMPE212 - Prof. McLeod

Aside – Polymorphism (A Quick Peek) We will learn more about this important aspect of OOP later, but for now: Polymorphism is when a pointer of a parent class type ends up pointing to different child class objects at runtime. Also called “dynamic binding”. The process also must satisfy early binding: Early Binding is satisfied when the parent class also owns the method that will end up being invoked from the morphed child class objects. The use of interfaces and abstract classes can make it easier to code for early binding. Winter 2018 CMPE212 - Prof. McLeod

Interfaces in Java 7 and Older Winter 2018 CMPE212 Interfaces in Java 7 and Older Interfaces contain: constant attributes and/or abstract methods. Abstract methods consist of just the method header – there is a semi-colon instead of { }. (Abstract classes can contain both concrete and abstract methods, along with any kind of attribute.) Winter 2018 CMPE212 - Prof. McLeod Prof. Alan McLeod

Interfaces in Java 8 In addition to the above, we can also have: Default methods. Static methods. Winter 2018 CMPE212 - Prof. McLeod

Java 8 Interface Default Methods Must have the keyword default at the beginning of the method header. These methods will not be empty, but can contain implementation code. This will be a “default” implementation for this method. It can be overridden or inherited by a class implementing the interface, or by another interface extending this interface. Winter 2018 CMPE212 - Prof. McLeod

Java 8 Interface Default Methods, Cont. The purpose is to reduce the work required when methods are added to interfaces in existing hierarchies. You do not *have* to implement the default method. (This also makes interfaces more like abstract classes…) Winter 2018 CMPE212 - Prof. McLeod

Java 8 Interface static Methods They behave just the same way a static method behaves in a normal class. However, static methods are not inherited by classes implementing the interface. Also simplifies design. Winter 2018 CMPE212 - Prof. McLeod

Interfaces in Java 9 (The latest version of Java. The one we are using.) In addition to the options listed above, Java 9 interfaces can have private static and normal private methods, just like those in a normal class. The purpose is to simplify the construction of the interface by providing a means to eliminate common code in other methods. Winter 2018 CMPE212 - 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? Winter 2018 CMPE212 - 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. Winter 2018 CMPE212 - 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. Winter 2018 CMPE212 - 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. Winter 2018 CMPE212 - 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… Winter 2018 CMPE212 - 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. Winter 2018 CMPE212 - Prof. McLeod