Thursday, 10 March 2011

UML: Inheritance and Java

One problem you often see when dealing with UML is that some developers don’t get the idea that what you see in class diagram should translate directly into code they draw a class diagram and write some code and neither bare any resemblance to each other. This, and other blog forthcoming blogs in the series, demonstrate the link between diagrams and code.

  /**
   * Class that is our generalization: the superclass
   */
 
public class MyTestClass implements MyInterface {

   
/** Some instance variable */
   
private String att1;

   
/**
     * Some operation that's not defined by the interface
     *
     *
@return 1;
     */
   
public int operation1() {
     
return 1;
   
}

   
/**
     *
@see uml.InterfaceReaisation.MyInterface#method1(java.lang.String)
     */
   
@Override
   
public String method1(String parameter) {
     
return null;
   
}

   
/**
     *
@see uml.InterfaceReaisation.MyInterface#method2(char)
     */
   
@Override
   
public String method2(char param2) {
     
return null;
   
}
  }

 
/**
   * This class is a subclass of MyTestClass - i.e. it inherits from it.
   */
 
public class MySubclass extends MyTestClass {

   
private double attribute;

   
/**
     * A subclass method
     */
   
public void subclassMethod() {

    }
  }

No comments: