Docsity

Object-Oriented Programming: Abstract Classes, Method Overriding, and Java, Slides of Banking and Finance

Toggle sidebar
bg1
23
Abstract Classes and Methods
An operation should be declared to exist at the highest
class in the hierarchy where it makes sense
•The operation may be abstract (lacking implementation)
at that level
• If so, the class also must be abstract
—No instances can be created
—The opposite of an abstract class is a concrete class
•If a superclass has an abstract operation then its subclasses
at some level must have a concrete method for the
operation
—Leaf classes must have or inherit concrete methods for
all operations
—Leaf classes must be concrete
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Object-Oriented Programming: Abstract Classes, Method Overriding, and Java and more Slides Banking and Finance in PDF only on Docsity!

23

Abstract Classes and Methods

An operation should be declared to exist at the highest class in the hierarchy where it makes sense

  • The operation may be abstract (lacking implementation) at that level
  • If so, the class also must be abstract —No instances can be created —The opposite of an abstract class is a concrete class
  • If a superclass has an abstract operation then its subclasses at some level must have a concrete method for the operation —Leaf classes must have or inherit concrete methods for all operations —Leaf classes must be concrete

24

Overriding

A method would be inherited, but a subclass contains a new version instead

  • For restriction —E.g. scale(x,y) would not work in Circle
  • For extension —E.g. SavingsAccount might charge an extra fee following every debit
  • For optimization —E.g. The getPerimeterLength method in Circle is much simpler than the one in Ellipse

26

Dynamic binding

Occurs when decision about which method to run can only be made at run time

  • Needed when: —A variable is declared to have a superclass as its type, and —There is more than one possible polymorphic method that could be run among the type of the variable and its subclasses

27

2.7 Concepts that Define Object Orientation

The following are necessary for a system or language to be OO

  • Identity —Each object is distinct from each other object, and can be referred to —Two objects are distinct even if they have the same data
  • Classes —The code is organized using classes, each of which describes a set of objects
  • Inheritance —The mechanism where features in a hierarchy inherit from superclasses to subclasses
  • Polymorphism —The mechanism by which several methods can have the same name and implement the same abstract operation.

29

The Basics of Java

History

  • The first object oriented programming language was Simula- —designed to allow programmers to write simulation programs
  • In the early 1980’s, Smalltalk was developed at Xerox PARC —New syntax, large open-source library of reusable code, bytecode, platform independence, garbage collection.
  • late 1980’s, C++ was developed by B. Stroustrup, —Recognized the advantages of OO but also recognized that there were tremendous numbers of C programmers
  • In 1991, engineers at Sun Microsystems started a project to design a language that could be used in consumer ‘smart devices’: Oak —When the Internet gained popularity, Sun saw an opportunity to exploit the technology. —The new language, renamed Java, was formally presented in 1995 at the SunWorld ’95 conference.

30

Java documentation

Looking up classes and methods is an essential skill

  • Looking up unknown classes and methods will get you a long way towards understanding code

Java documentation can be automatically generated by a program called Javadoc

  • Documentation is generated from the code and its comments
  • You should format your comments as shown in some of the book’s examples —These may include embeded html

32

Characters and Strings

Character is a class representing Unicode characters

  • More than a byte each
  • Represent any world language

char is a primitive data type containing a Unicode character

String is a class containing collections of characters

    • is the operator used to concatenate strings

33

Arrays and Collections

Arrays are of fixed size and lack methods to manipulate them

ArrayList is the most widely used class to hold a collection of other objects

  • More powerful than arrays, but less efficient

Iterators are used to access members of Vectors

  • Enumerations were formally used, but were more complex a = new ArrayList(); Iterator i = a.iterator(); while(i.hasNext()) { aMethod(i.next()); }

35

Exceptions

Anything that can go wrong should result in the raising of an Exception

  • Exception is a class with many subclasses for specific things that can go wrong Use a try - catch block to trap an exception try { // some code } catch (ArithmeticException e) { // code to handle division by zero }

36

Interfaces

Like abstract classes, but cannot have executable statements

  • Define a set of operations that make sense in several classes
  • Abstract Data Types A class can implement any number of interfaces
  • It must have concrete methods for the operations You can declare the type of a variable to be an interface
  • This is just like declaring the type to be an abstract class Important interfaces in Java ’ s library include
  • Runnable, Collection, Iterator, Comparable, Cloneable

38

Access control

Applies to methods and variables

  • public —Any class can access
  • protected —Only code in the package, or subclasses can access
  • (blank) —Only code in the package can access
  • private —Only code written in the class can access —Inheritance still occurs!

39

Threads and concurrency

Thread:

  • Sequence of executing statements that can be running concurrently with other threads To create a thread in Java:
    1. Create a class implementing Runnable or extending Thread
    1. Implement the run method as a loop that does something for a period of time
    1. Create an instance of this class
    1. Invoke the start operation, which calls run

41

Programming style …

Comment extensively

  • Comment whatever is non-obvious
  • Do not comment the obvious
  • Comments should be 25-50% of the code

Organize class elements consistently

  • Variables, constructors, public methods then private methods

Be consistent regarding layout of code

42

Programming style …

Avoid duplication of code

  • Do not ‘clone’ if possible —Create a new method and call it —Cloning results in two copies that may both have bugs - When one copy of the bug is fixed, the other may be forgotten