Universidad Carlos III de Madrid

Ingeniería de Telecomunicación

Enero-Mayo 2010 / January-May 2010

Object-Orientation & Inheritance

Objectives Section Learning Objectives

Learning Objectives for Introduction to Java

The general goal of this part of the session is to remind you of the things that you should have learnt in the first semester, and to propose some resources and material where you can reinforce that knowledge, if necessary. If you have considerable lacks, take into account that you will need more time to get to this goals than the estimated in the session.

  • Define, describe and correctly program classes and objects. (Bloom 3*)

  • Program easy algorithms using loops. (Bloom 3*)

  • Represent and process easy data structures using arrays. (Bloom 3*)

Learning Objectives for Object Based Programming

  • Define the concept of inheritance. (Bloom 1*)

  • Describe the syntax to use inheritance. (Bloom 1)

  • Draw UML diagrams to represent inheritance. (Bloom 1*)

  • Explain which are the members (attributes and methods) of a class (subclass) that inherits from another class (superclass). (Bloom 2*)

  • Explain how to build hierarchies of classes. (Bloom 2*)

  • Given a textual description, draw diagram of the corresponding class inheritance hierarchy. (Bloom 3)

  • Given a class hierarchy, program the participating classes. (Bloom 3*)

  • Given a set of classes with inheritance dependencies, extract the class hierarchy. (Bloom 3*)

  • Solve theoretical questions regarding inheritance. (Bloom 3*)

  • Given a program using inheritance identify the following elements: superclass, subclass, overridden method, overloaded method. (Bloom 4)

  • Solve problems using inheritance, departing from an initial design for the solution proposed in the text, and describing the system to model. (Bloom 5)

Learning Objectives for Object Oriented Programming

  • Define the concept of method overwriting. (Bloom 1*)

  • Define the concept of polymorphism. (Bloom 1*)

  • Explain how and when type compatibility applies in a class hierarchy (both upcasting and downcasting).

  • Explain how method overriding works, i.e. how the system decides which method to invoke in each case for a particular object. (Bloom 2*)

  • Give examples of type compatibility (upcasting and downcasting), and program them in Java. (Bloom 3*)

  • Program and test a class hierarchy with an example of method overriding. (Bloom 3*)

  • Given a program using polymorphism, identify the places where it helps to reuse code. (Bloom 4)

  • Define the concept of an abstract method and an abstract class. (Bloom 1*)

  • Define the concept of interfaces. (Bloom 1*)

  • Describe the syntax to declare and to implement interfaces. (Bloom 1*)

  • Draw UML diagrams to represent interface implementation. (Bloom 1*)

  • Explain why it is not possible to create objects of an abstract class. (Bloom 2*)

  • Explain the difference between an abstract class and an interface. (Bloom 2*)

  • Explain why multiple inheritance is problematic (and not allowed in Java) but multiple implementation of interfaces is not problematic and it is allowed. (Bloom 2*)

  • Explain which are the polymorphic relations inside a class hierarchy (using inheritance and/or interface implementation). (Bloom 2*)

  • Program an example of a class with an abstract method. (Bloom 3*)

  • Program a class that inherits from an abstract class. (Bloom 3)

  • Program a class that implements an interface. (Bloom 3*)

  • Program a class that both implements an interface and extends another class. (Bloom 3*)

  • Solve theoretical questions regarding interface implementation. (Bloom 3*)

  • Given a program using inheritance and/or interface implementation, identify the following elements: superclass, subclass, interface, interface implementation, overridden method, overloaded method. (Bloom 4)

  • Explain when to use class inheritance or interface implementation to represent polymorphism. (Bloom 4)

  • Solve problems using inheritance and/or interfaces, departing from an initial design for the solution proposed in the text, and describing the system to model. (Bloom 5)

Lecture Section Session 1 (lecture): Object-Orientation & Inheritance (I)

Material

Lab Section Session 2 (lab): Object-Orientation & Inheritance (I)

Material

Material (with solutions)

Lecture Section Session 3 (lecture): Object-Orientation & Inheritance (II)

Material

Lab Section Session 4 (lab): Object-Orientation & Inheritance (II)

Material

Material (with solutions)

Lecture Section Session 5 (lecture): Object-Orientation & Inheritance (III)

Material

Lab Section Session 6 (lab): Object-Orientation & Inheritance (III)

Material

Material (with solutions)

Learning Pills Section Learning Pills

¿DID YOU KNOW THAT...?

As part of its declaration and initialization, you have to specify the size of an array, and this is one of its main advantages / disadvantages with respect to other structures, such as classes implementing the List interface or linked lists.

¿DID YOU KNOW THAT...?

It's advisable to label the attributes of a class as private and access them by means of methods.

¿DID YOU KNOW THAT...?

The following code is wrong:

		
public boolean isCarnivore (Animal animal){
 boolean result;
 if (animal.isClassified()){
  if (animal.getType().equals(Animal.CARNIVORE)){
   result = true;}
  else{
   if (animal.getType().equals(Animal.OMNIVORE)){
    result = true;}
   else{ result = false;}}}
 return result;}
					

¿DID YOU KNOW THAT...?

The following code is wrong:

		
public boolean isCarnivore (Animal animal){
 try{ boolean result= false;	
  if (animal.isClassified()){	
   if (animal.getType().equals(Animal.CARNIVORE)){
    result = true;}											
   else{		
    if (animal.getType().equals(Animal.OMNIVORE)){
     result = true;}			
    else{result = false; }}}	
 } catch (AnimalException e){// Handling exception}}
 return result;}
					

¿DID YOU KNOW THAT...?

As part of the implementation of inheritance, the constructor of the child class (or derived) calls to the constructor of the base class. if you don't use the constructor by default in the base class, it will be necessary to state it explicitly in the derived class.

¿DID YOU KNOW THAT...?

Inheritance is related to a design technique called Incremental Design, which consists on increasing the functionality of the starting point approach gradually.

¿DID YOU KNOW THAT...?

The method instanceOf is very useful to classify objects in the field of inheritance.

¿DID YOU KNOW THAT...?

Constants in Java are defined with the final and static identifiers.

¿DID YOU KNOW THAT...?

Java allows overloaded methods.

¿DID YOU KNOW THAT...?

You have the possibility of overriding or overloading a method in a derived class to suit your needs.

¿DID YOU KNOW THAT...?

Two usual ways to reuse objects are inheritance and composition.

¿DID YOU KNOW THAT...?

You can identify and differentiate whether to use inheritance or composition when you design a class.

¿DID YOU KNOW THAT...?

Inheritance, abstraction and definition of interfaces are implementation techniques highly related to conceptual design.

¿DID YOU KNOW THAT...?

An abstract class can have attributes, but these by definition have to be labeled as static with the risks involved.

¿DID YOU KNOW THAT...?

The use of interfaces is a useful technique in the modular design and simplifies a team work.

¿DID YOU KNOW THAT...?

Following code is correct:

		
interface readFileDevice{							
 String readNextLine(String filePath);
 List<String> readNextParag(String fPath);							
}

public abstract class FileReader implements
                             readFileDevice{							
 String readNextLine(String filePath){
  <code here>	
 }
 abstract List<String> readNextParag(String fPath);							
}
					

¿DID YOU KNOW THAT...?

Dedicate a reasonable amount of time to model, define and identify the different features of the program to be implemented, and foresee potential problems that may arise, is a good practice that will save you time and provide flexibility and traceability to your programs.

¿DID YOU KNOW THAT...?

Following code is wrong:

		
interface fileRead{						
 String readNextLine(String filePath);
 List<String> readNextBlock(String filePath);}
interface multimediaRead{						
 MultimediaFile readNextBlock(String filePath);}
public class reader 
       implements fileRead, multimediaRead{	
 String readNextLine (String filePath){
  <code here>}
 List<String> readNextBlock(String filePath)
 {...}
 MultimediaFile readNextBlock(String filePath){...}}
					

¿DID YOU KNOW THAT...?

The following code is wrong:

						
boolean found = false;
int i=0;
int practiceConnected = 0;
while (i<subjects.length)){
 if (connectedUsersSubjects[i].equals(subject)==0){
  practiceConnected = practiceConnected  + 1;}
 else{
  i=i+1;}	
}	
return found;
					

¿DID YOU KNOW THAT...?

In the following snippet of code, value = 6, is not the desirable result.

		
boolean multiple2 = false;	
boolean multiple3= false;
if (value%2==0){
 multiple2=true;	
}
else{
 if (value%3==0){	
  multiple3=true;		
 }	
}