Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sixth Lecture ArrayList Abstract Class and Interface

Similar presentations


Presentation on theme: "Sixth Lecture ArrayList Abstract Class and Interface"— Presentation transcript:

1 Sixth Lecture ArrayList Abstract Class and Interface

2 ArrayList A class consisting of a private array data structure
It does not require an initial size as the array data structure requires. ArrayList can grow and shrink during program execution. Stores objects only, will not store primitive data (integers, doubles, booleans).

3 ArrayList Wrapper classes are available for primitive data which allows for primitive data to be stored in ArrayLists. The Arraylist method add(object) will add elements to ArrayList data structure.

4 ArrayList set method allows for objects to be placed at specified locations in the ArrayList assuming the location currently contains an object. Any location containing a NULL will not allow the set method to insert an object.

5 ArrayList Advantage: import java.util.ArrayList;
Can grow or shrink (arrays can’t) import java.util.ArrayList;

6 ArrayList Disadvantage: Less efficient than arrays
Lacks familiar [] syntax (Java limitation, supported by C++) Base type must be an object (not primitive) type Can have ArrayList of Integer but can’t have an ArrayList of int. Less of a problem w/ automatic boxing and unboxing Java limitation, supported by C++

7 ArrayList class Define an ArrayList of Strings:
ArrayList<String> list = new ArrayList<String>(); Define an ArrayList of Employees: ArrayList<Employee> empList = new ArrayList<Employee>( 20 ); Initial capacity is specified as 20.

8 ArrayList class Set an element: Get an element:
list.set( 12, “Hi, Mom.” ); Get an element: System.out.println( list.get( 12 ) ); String s = list.get( 5 );

9 ArrayList class Add an element: add( value ) form list.add( “One” );
list.add( “Two” ); list.add( “Three” ); “One” is at position 0, “Two” is at position 1, and “Three” is at position 2.

10 ArrayList class Add an element: add( i, value ) form
i must be a used position or the first unused position list.add( “One” ); list.add( “Two” ); list.add( “Three” ); list.add( 1, “Fred” ); “One” is at position 0, “Fred is at position 1, “Two” is at position 2, and “Three” is at position 3.

11 Classes in java Abstract Class Interface Concrete Class

12 Abstract Class Are used to declare common characteristics of subclasses. An abstract class cannot be instantiated. It can only be used as a superclass for other classes that extend the abstract class. Abstract classes are declared with the abstract keyword. Abstract classes are used to provide a template or design for concrete subclasses down the inheritance tree.

13 Example for Abstract Class
abstract class Shape { public String color; public Shape() { } public void setColor(String c) { color = c; } public String getColor() { return color; } abstract public double area(); }

14 public class Point extends Shape
{ static int x, y; public Point() { x = 0; y = 0; } public double area() { return 0; } public double perimeter() { return 0; } public static void print() { System.out.println("point: " + x + "," + y); } public static void main(String args[]) { Point p = new Point(); p.print(); } }

15 Interface can be used to define a generic template and then one or more abstract classes to define partial implementations of theinterface. Interfaces just specify the method declaration (implicitly public and abstract) and can only contain fields (which are implicitly public static final).  Interface definition begins with a keyword interface. An interface like that of an abstract class cannot be instantiated. Multiple Inheritance is allowed when extending interfaces i.e. one interface can extend none, one or more interfaces. Java does not support multiple inheritance, but it allows you to extend one class and implement many interfaces.

16 Example of Interface interface Shape { public double area(); public double volume(); }

17 public class Point implements Shape { static int x, y; public Point() { x = 0; y = 0; } public double area() { return 0; } public double volume() { return 0; } public static void print() { System.out.println("point: " + x + "," + y); } public static void main(String args[]) { Point p = new Point(); p.print(); } }

18 Note If a class that implements an interface does not define all the methods of the interface, then it must be declared abstract and the method definitions must be provided by the subclass that extends the abstract class.

19 What is the Different between Interface and Abstract Class?
Home Work What is the Different between Interface and Abstract Class?

20 Concrete Class has definition A class that can have instances
Conrete class are those classes which can b instantiated unlike Abstract classes which can't be instantiated

21 Collections in Java Arrays Collections (also called containers)
Has special language support Collections (also called containers) Collection (i) Set (i), HashSet (c), TreeSet (c) List (i), ArrayList (c), LinkedList (c) Map (i), HashMap (c), TreeMap (c)

22 Arrays: Advantages Disadvantages
An array know the type it holds, i.e., compile-time type checking. An array know its size, i.e., ask for the length. An array can hold primitive types directly. Disadvantages An array can only hold one type of objects (including primitives). Arrays are fixed size

23 Overview of Collection
A collection is a group of data manipulate as a single object. Corresponds to a bag. The Collections API is a mechanism for manipulating object references, not manipulate primitives.

24 Collections Arrays are used to hold groups of specific type of items
Collections (container) designed to hold generic (any) type of objects Collections let you store, organize and access objects in an efficient manner.

25 Java Collection Framework hierarchy, cont.
Set and List are sub-interfaces of Collection.

26 Some Legacy Collection Types
ArrayList Enumeration Vector HashTable – self study


Download ppt "Sixth Lecture ArrayList Abstract Class and Interface"

Similar presentations


Ads by Google