Personal tools
You are here: Home Classes Fall 2004 - Spring 2005 CS 151 Object-oriented Programming in Java
Navigation
Log in


Forgot your password?
« May 2008 »
Su Mo Tu We Th Fr Sa
123
456789 10
11121314151617
18192021222324
25262728293031
 
Document Actions

Object-oriented Programming in Java

by admin last modified 2005-05-11 18:13

Object-oriented Programming in Java

Some review questions on classes and objects

What is an object?


What is a class?


What are some of the advantages of object-oriented programming?


What steps might a programmer follow in order to design and write an object-oriented application?

Everything in Java is defined within classes.  Is it possible for a Java program to run without ever creating any objects?


A class may have instance variables and methods.  When we write a subclass of a given class (i.e. extend the class), how can we do this?


Assignment compatibility and type casting


Consider the following situation:  We have a Vehicle class.  One of its subclasses is SUV.

Which (if any) of the following statements will lead to an error?

Vehicle vehicle = new SUV();
SUV suv = new Vehicle();

Suppose Vehicle has a "drive" method which is overridden by SUV.  Which version of the drive method will be called by the following code:

Vehicle v = new SUV();
v.drive();

What if SUV has a "tow" method not present in Vehicle, defined as

void tow(Vehicle vehicle);

What will happen in the code below?

Vehicle vehicle = new SUV();
Vehicle beetle = new Beetle();
vehicle.tow(beetle);

In a collection such as ArrayList, we can store any type of Object.  Consider the following code:

ArrayList list = new ArrayList();
list.add(new SUV());

Is the following statement legal?

SUV suv = list.remove(0);


Abstract classes


What is an abstract class?

What is the purpose of an abstract class?

Did you use any abstract classes in your work in CS 150?


Interfaces


The concept of an interface will be very important to us in this course, as we study various abstract data types (e.g., stacks, queues, lists, etc.).  Consider the following:



The key idea is to separate the applications of an ADT from its implementation.  The interface is the glue that holds them together.  As long as the application and the implementation agree on the interface, they don't need to know anything else about each other.  This makes it possible to
  • divide work among different programmers (modularity)
  • save code in libraries for future use (reusability)
  • substitute one implementation for another -- applications will still work
  • use a given implementation in a variety of applications

This model is used throughout computer science, not just in object-oriented programming or data structures.  Consider for example, Intel's Pentium architecture.  The architectural specifications are published by Intel and form an interface between the CPU hardware and the operating system software.  As a result,
  • We can run different operating systems (Windows, linux) on an Intel CPU.
  • Intel-compatible CPUs can be produced by other vendors (e.g., AMD Athlon) and run the same software that runs on Pentium machines.

Application designers are freed from knowing anything about the details of the implementation.  They can trust the library code to obey the interface.
Implementers are freed from knowing anything about the details of any application.  As long as they program the interface correctly, they've done their job.

Question:  Who writes the interface?


What is meant by the syntax and semantics of an interface?

The syntax of an interface defines the name and parameter list of each method in the interface.

The semantics of an interface defines what each of the methods actually does.


 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: