1 / 14

Abstract Class

Abstract Class. On Thursday, we learned what an interface is – a class that contains abstract (empty) methods, and can also contain constants. An abstract class is similar, but one major difference is that it can contain methods that actually contain code.

louvain
Download Presentation

Abstract Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Abstract Class • On Thursday, we learned what an interface is – a class that contains abstract (empty) methods, and can also contain constants. • An abstract class is similar, but one major difference is that it can contain methods that actually contain code. • See Thursday’s “confusing terminology” slide -- another way of saying this is that an abstract class can implement methods, while an interface cannot. • Like an interface, an abstract class CANNOT be insantiated. (In other words, you cannot, in a client, create an object of an abstract class.)

  2. public abstract class Sample { public int sum (int num1, int num2) { return (num1 + num2) } } /* note that this abstract class contains a method with actual code in its body, which is not allowed in an interface. */

  3. Interface vs. Abstract Class • You cannot instantiate (create an object of) an abstract class, but you can extend an abstract class, and then use its methods. • So, what’s the point of an abstract class? Why not just use an interface, if you want to create a “contract” that programmers must follow? • The answer is: If a bunch of programmers will be using the same method, in the same way, then it makes more sense to put this method in an abstract class and have them extend this class. • But, if those programmers will be using the same method signature, but writing code for the method body in different ways, then it makes more sense to put that method in an interface and have them implement that interface.

  4. We learned on Thursday that all methods and variables in an interface must be public. (it wouldn’t make sense to have a private method in an interface, because where would this empty method be called from?) • An abstract class, however, can have private methods and variables.

  5. Sorting • Sorting is the process of arranging the elements in an array in a particular order • The sorting process is based on specific values – examples: • sorting a list of test scores in ascending numeric order • sorting a list of people alphabetically by last name • There are many algorithms for sorting an array • These algorithms vary in efficiency & speed

  6. Big-O Notation • The efficiency/performance of a sorting algorithm is measured in “Big O” notation • For example: if an algorithm has O(6n) efficiency, that means that the maximum number of “moves” necessary to sort the array equals 6 times the number of elements in the array.

  7. You need to know the basics of these 4 sorting algorithms: • Selection Sort 2) Insertion Sort 3) Merge Sort 4) Quick Sort

  8. Selection Sort • find the smallest element in the array • swap it with the first element • find the next-smallest element in the array • swap it with the second element • repeat until all values are in their proper places Efficiency: O(n^2)

  9. Example: original array: 3 9 6 1 2 smallest is 1: 1 9 6 3 2 smallest is 2: 1 2 6 3 9 smallest is 3: 1 2 3 6 9 smallest is 6: 1 2 3 6 9

  10. Insertion Sort • pick an item and insert it into its proper place in a sorted sublist • repeat until all items have been inserted Efficiency: O(n) An example: original: 9 3 6 1 2 insert 3: 3 9 6 1 2 insert 6: 3 6 9 1 2 insert 1: 1 3 6 9 2 insert 2: 1 2 3 6 9

  11. Merge Sort • Divides a list in half, recursively sorts each half, and then combines the two lists • At the deepest level of recursion, one-element lists are reached • A one-element list is already sorted • The work of the sort comes in when the sorted sublists are merged together • Efficiency: O(n log n)

  12. Quick Sort • Chooses a pivot value, then partitions the list into two sublists, (one list contains everything smaller than the pivot, the other contains everything larger), then recursively sorts each sublist • Unlike a merge sort, a quick sort does most of its work when it divides the list • It doesn’t need to combine sublists after the recursive steps; the list is already sorted at that point • Also, unlike a merge sort, the 2 sublists do not have to be the same size • Efficiency: O(n log n)

  13. Review • Selection Sort: Swaps numbers in a list until the list is sorted • Insertion Sort: Sorts the first 2 #s in the list, then the first 3, then the first 4, etc… • Merge Sort: cuts list in half, sorts them, then combines the 2 lists • Quick Sort: cuts list in half using a pivot value; sorts each sublist, no need to combine the lists at the end since they are already sorted

  14. Assignment • Open BarronsPractice Test 1. Complete #1-20 (skip 12). Record answers in a Word doc called BarronsPracTest1Answers.

More Related