abstract classes
has at least one abstract method
defined to represent a general category.
* Partially implemented (some methods, some attributes).
* Explicitly leaves some methods unimplemented
To specify actions of a class - telling subclass which methods to implement
cannot create an instance of the class, have to first derive it and define missing methods.
Postpone the definition of a method to the derived classes. Create functionality that derived classes can inherit or override , while requiring some functionality to still be implemented
class derived from an abstract class can be abstract as well
provide partial abstraction
public abstract class Shape {
public abstract double area();
}
concrete classes
specialised versions/descendants of abstract class
Derived class / child class
* They inherit methods and variables from the abstract class.
* They are required to define unimplemented methods
abstract method
placeholder for a method that will be fully defined in a descendent class.
has heading, but no method body.
body is defined in derived classes
Have to override (define) it in a concrete descendent class.
* Cannot be private (private methods cant be inherited / overridden) or static (static methods cant be overridden.)
public abstract double perimeter();
interfaces
specifies which methods a class must have (no method definitions, only headers & constant definitions)
definition of an action that a class possesses.
Shared functionality, but not shared implementations.
* An interface in Java is a blueprint for a class
Interfaces dont have constructors, nor instance variables
You implement an interface
provides full abstraction
class that implements an interface is required to provide definitions for all the interface methods
a type satisfied by any class implementing it.
an interface may be derived from a base interface: extending the interface.
can be implemented by abstract class, child class
interface & its methods must be declared public.(Cannot lower access scope!)
can implement multiple interfaces
public class N implements y, z
can be a parameter for a method
extends Y implements X
public interface Ordered
public class HourlyEmployee implements Ordered
interfaces vs abstract class
Both are types.
* Can be used as method parameter types.
* Can be used as variable types.
* Neither can be used to create objects.
interface :
No methods implemented
- all only have headings.
* Defines functionality, but
does not implement any.
* Methods can only be public.
Ideal for specified set of
functions for classes, with class-
specific implementations.
abstract:
Some methods implemented,others only have headings.
* Defines some functionality, implements other.
* Abstract methods can be anything except private
Ideal for hierarchy of functions among classes, with some common implementations.
marker interfaces
empty
used to tag a class as having a specific property/behaviour, but it won’t necessarily be enforced.
Code can check if an object type implements a marker interface.
object instanceof InterfaceName
-Cloneable : empty, used to indicate that clone() can be used safely.
-Serializable: empty, type tag indicates that it implements file I/O in a particular way
Comparable
Provides sorting functionality to any class.
* Has only one method heading that must be implemented:
public int compareTo(Object other);
Returns:
○ negative if this object precedes other
○ zero if equal
○ positive if this object follows other
arraylist
same purpose as arrays, except ArrayList length can change while program is running (size dynamic)
* Implemented using array instance variable. When this hidden array is full, a new larger hidden array is created and the data is transferred to this new array.
Base type of an ArrayList must be class type (or other reference type) NOT primitive
ArrayList<BaseType> l = new ArrayList<BaseType>()
nitial capacity can be specified ArrayList<String> l2 = new ArrayList<String>(20);
More efficient if you know the size requirement ahead of time.
* Does not limit the size to which ArrayList can eventually grow.
* If unspecified, default initial capacity is 10.</String></String></BaseType></BaseType>
methods of arraylist
add(BsseType newElement)
add(int index, BaseType newElement) //index must be >=0 and <= current size of AL
get(int index)
set(int index, BaseType newEl) //returns element prev. at that position
remove(int index)
protected removeRange(int from, int to)
remove(Object theElement)
clear()
contains(Object target)
int indexOf(Object target)
int lastIndexOf(Object target)
size()
isEmpty()
Object[] toArray() //returns array of all elements on list in order
clone() // returns shallow copy
Generics
Create classes that work with different data types.
* The idea is to allow type (Integer, String, … etc., and user-
defined types) to be a parameter.
* Generic programming with a type parameter enables code to
be written that applies to any class
type plugged in must be reference/class type not primative
Cannot create arrays of generic classes
public class Name<T> { //T is the type paramater</T>
private T data //instance var
public void setData(T newData){
data = newData; }
public T getData() {
return data;}
constructor headings do not include type
public Name(T data) {
hgj}
public class Pair<T>
public Pair(T firstItem, T secondItem)
Pair<String> p = new Pair<String>("A", "B");</String></String></T>
public class ClassName<T1, T2>
A generic class definition can have any number of type parameters,
each parameterizing a type that must be specified (types can be the
same or different)
ClassName<String, Integer> c1 = new ClassName<String, Integer>(“alpha”, 42);
Inheritance and interfaces can be used with generic classes
generic bounds
restrict the types that can be plugged in for type parameter
* public class ClassName<T>
Only descendent classes of BaseClass can be plugged in for T
* public class ClassName<T>
Only classes implementing Comparable interface can be plugged in for T.
* public class ClassName<T extends BaseClass & Comparable & Ordered>
A bounds expression may contain multiple interfaces and up to one class.
Inside generic type parameters (<>), you must use extends for both classes and interfaces. The keyword implements cannot be used within the angle brackets.</T></T>
generic methods
Methods can be generic, having their own type parameters.
Method heading: public <T> T gen(T[] a)
Method invoke: String s = object.<String>gen(c);</String></T>
inner classes
classes defined within other classes.
* The class that includes the inner class is called the outer class.
* Inner class can be defined anywhere in an outer class, but
convention is at the start or end of a class definition.
* Compiling a class with one (or more) inner classes produces
multiple .class files.
They can make the outer class more self-contained.
* Outer and inner class have access to each other’s private
methods and instance variables.
Very useful as helping classes (should then be private)
An inner class definition is a member of a class - just like
variables/methods
If the inner class is private, then the inner class cant be accessed by name outside definition of outer class.
In the definition of an inner class method, you can access
private variables and methods of the outer class.
* In the definition of an outer class method, you can access
private variables and methods of the inner class.
(but you might have to create an object of the inner class)
static inner classes
Static Inner Classes are static for the outer class.
* A static inner class has no connection to an outer class object
Instance variables of the outer class cannot be referenced.
* Non-static methods of the outer class cannot be invoked.
(Both of these are connected to an instance of the outer class.)
you can have non-static methods in static inner classes
Static Inner Classes:
* No reference to an outer class instance or instance members –
can be instantiated without an instance of the outer class
used in contexts that are without an instance of the outer class e.g.
within a static method of the outer class.
If the inner class must have static members.
The entire body of a non-static inner class is not within a static
scope and therefore you can’t have static members in there
non static inner classes
Keep a reference to an instance of the outer class and can
access its instance members.
you cant have static methods in non-static inner classes
public inner classes
If an inner class is public, it can be used outside the outer class
In the case of a non-static inner class, it must be created using
an object of the outer class.
A public static inner class can be instantiated – in a static context
relative to the outer class. It will not belong to an instance of the
outer class.
OuterClass.InnerClass innerObject = new OuterClass.InnerClass();
nested inner classes
You can define an inner classes within an inner class:
* The rules are the same as before, but the names get longer.
* Given class A, which has public inner class B, which has public
inner class C, then the following is valid:
A a = new A();
A.B b = a.new B();
A.B.C c = b.new C();
inheritance & inner classes
outer class can be a derived class.
inner class can be a derived class
Any DerivedClass of OuterClass will automatically have InnerClass as an inner class.
DerivedClass cannot override InnerClass.
anonymous class
a class without a name.
* Defined inside {…} with the new operator.
* Useful when you only use a class once.
* Sometimes anonymous classes are assigned other types (e.g.
interfaces such as Comparable).
* All anonymous classes are examples of inner classes
linked list
single chain of nodes , each connected to the next by a link
simplest linked data structure
first node: head node
end node: end marker
each node is object of node class
each node contains data & link to another node
Data is stored as instance variable of the node type that contains a reference to the head node
Links are implemented as references to a node stored as an instance variable of the node type
A linked list object does not contain all nodes in the list directly, only contains head node.
The entire linked list can be traversed by starting at the head.
head node and every node of the list
contain a link instance variable that provides a
reference to the next node in the list
When head == null, the list is empty.
The last node’s link instance variable (end marker) is always null. This indicates that we have reached the end of the linked list
reference variables
Reference variables point out the object (the location in memory)
Classes, interfaces, arrays are all reference types.
Reference variable can also store null. By default, if no object is passed to a reference variable then it will store a null value.
traverse a linked list
add node to start of a linked list / push
head = new Node(newItem, head);
remove first node of a linked list