CS230 Tutorial Week 3x

Download Report

Transcript CS230 Tutorial Week 3x

CS230 Tutorial Week 3
Agenda
• Java language review.
• Inheritance and abstract classes
• Interfaces
• More questions on Assignment 1?
Relevant Chapters in Lang-ref
• Chapter 3 --- Lexical Structure
• 3.10 Literals
• Chapter 4 --- Types, Values, and Variables
• 4.3.3 The Class String
• Chapter 10 --- Array
• Chapter 14 --- Blocks and Statements
Lexical Elements
• Much easier if you understand compilers!
• First step of a compilation process --- tokenization.
• Which converts source code into lexical elements
•
•
•
•
Identifiers
Literals
Keywords and operators
Comments discarded
Literals
• Integers --- 1, 0x80
• Floats --- 0.01
• Booleans --- true, false
• Characters --- 'a'
• Strings --- "hello, world"
• Null --- null
Types
• Value Types (Primitive Types)
• int, float, Boolean
• NOT String
• Has value semantics --- copy value and equality checking
• Reference Types
• String
• Classes and interfaces
• Has reference semantics --- copy reference, check pointer
equality
Blocks and Control Statements
• Same as C/C++
• Differences from python are purely syntactical:
• Semicolons ;
• Braces {}
• Indentation-insensitive
Array
• T[] --- REFERENCE TYPE!
• (Very important) semantic difference from C/C++/C#
• Instantiated with new()
• Static array --- T[]
• Dynamic array --- ArrayList<T>
Inheritance
• Not as hard as you might have thought!
• “Inheritance” --- in many occasions a loosely-defined
concept.
• Basically means a subclass has inherited states and
behaviours from (the/a) superclass.
• Java --- the superclass --- no multiple inheritance
• A subclass subtypes (the/a) superclass.
However --- in Java
• The final arbiter of the Java language --- the language
reference.
• In the language reference inheritance has a clear and
unambiguous meaning.
• For details see Section 8.4.8 --- Very detailed rules of
what is inherited and what is not.
• Not all methods are inherited --- no private
• Specific rules concerning abstract classes and
overloading
Abstract classes
• Very simple concept! (Not as hard as you might think.)
• Any class that has an abstract method.
• Cannot be instantiated (just like interfaces).
• To instantiate --- must subclass.
Interfaces (Chapter 9 in Langref)
• (Conceptually) similar to abstract class --- with
(almost) every method abstract
• Almost --- can have static or default methods
• Similar to abstract classes --- cannot be instantiated
• Different to abstract classes --- a subclass can inherit
from multiple interfaces (but only one superclass).
• Java’s middle ground on multiple inheritance --interfaces only, not from superclasses.