Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Converting class diagrams to code

From: Foundations of Programming: Object-Oriented Design

Video: Converting class diagrams to code

We're focused on the ideas and concepts of object orientation here, but we never forget that the entire point is that we'll convert this into a programming language. And it can be useful to see just how close many of the languages are with these same concepts. So I have a very simple UML class diagram here with a couple of attributes and a couple of behaviors, because I want something I can demonstrate in just a few lines of code in several different languages. So, this will be a Spaceship class. It has two attributes, one is called name which is a String, and the other is shieldStrength which I've said here will be an Integer.

Converting class diagrams to code

We're focused on the ideas and concepts of object orientation here, but we never forget that the entire point is that we'll convert this into a programming language. And it can be useful to see just how close many of the languages are with these same concepts. So I have a very simple UML class diagram here with a couple of attributes and a couple of behaviors, because I want something I can demonstrate in just a few lines of code in several different languages. So, this will be a Spaceship class. It has two attributes, one is called name which is a String, and the other is shieldStrength which I've said here will be an Integer.

Name is public and shieldStrength is private, meaning it would be hidden from other objects directly. And it has two operations, two behaviors. They're both public. Fire is going to return us a string and reduceShields returns nothing or at least nothing that's written in this diagram and takes an Integer, very straightforward, very simple. Obviously, a full spaceship class would have a lot more behaviors than this, but it will allow us to illustrate some of these concepts. So, to take this into Java, we'd have this. We use the words public class Spaceship to actually create the entire spaceship class, opening and closing curly braces to contain it.

Then we have a section called instance variables, the two Forward Slashes representing a comment. Now, from what we'd call an attribute in a generic UML diagram or a conceptual model, we're more likely to call an instance variable in this case. These are variables that belong to an instance of a class, meaning that any objects created from this class will have their own copy of these variables. I've used the words public and private to refer to the visibility level we'd selected in the diagram and those are keywords in Java.

Now, if you're unfamiliar with Java or other Curly Brace-format languages, don't worry about the syntax here. It's really just about the idea, the kind of keywords we have, the way we would name these variables, the way we would name our methods, our behaviors. Now, if I took this into C# at the same level of complexity, it would look basically identical. C# and Java are very similar, indeed, and implement a lot of these ideas the same way. We have our two instance variables, we have our two methods, one called fire that returns a string, and one called reduceShields that takes an integer called amount and returns void, meaning it returns nothing.

If I looked at this in VB.NET, it would change the format away from the curly braces. Now, we'd start to see a lot of uppercase letters at the start of our keywords just because that is the convention in VB.NET. But otherwise, it's conceptually the same. If I the look at this class in Ruby, well, Ruby also leaves out the curly braces, and it has a simpler syntax because Ruby is a dynamic language. Meaning, it does not require as much information regarding or return types. So, for example, the At sign in front of the variable names name and shield_strength is how we say these are instance variables.

Ruby's convention for naming them would be to use the Underscore between multiple words rather than an uppercase letter. Technically speaking, because Ruby is so dynamic, we wouldn't even need to declare these variables like this. We would just use them if we needed them. Now, Objective-C becomes a bit more complex, and the classes in Objective-C as in C++ are split into two pieces, what's called the interface and the implementation. These are typically held in different files.

Now, unlike the other languages we've seen up to this point, Objective-C does not use the word class here, but the @ interface and @implementation keywords. And as you can see, there's quite a bit more that it throws into the mix. The biggest difference with these kind of languages is that we have the interface that says what exists. In fact, we have the comment here that says the method declarations are here, we're saying what methods exist, fire and reduceShields. But in the other file, the implementation, is where we say how they do what they do.

But what we're starting to run into even with this very simple example is the yes, but you really wouldn't do it that way syndrome. All the languages have their own habits, their own customs. What I wanted to show is that the process we've gone through does work across the languages, but the implementation details will be different. And rather than spend hours talking about all the specifics and syntax, I'm going to leave implementation to you, and continue to talk in generic object orientation terms, because even if you work in a language that has a substantially different implementation of some of these ideas, say, JavaScript or Lua or Go, that don't have official classes, their explanation of how to implement object orientation will refer to the classic concepts we're exploring here.

And we will cover a few more things about languages later on, and I'll point you to some other resources if you're interested in a specific feature of that language.

Show transcript

This video is part of

Image for Foundations of Programming: Object-Oriented Design
Foundations of Programming: Object-Oriented Design

47 video lessons · 54103 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 11m 35s
    1. Welcome
      1m 25s
    2. Who this course is for
      1m 15s
    3. What to expect from this course
      3m 6s
    4. Exploring object-oriented analysis, design, and development
      1m 41s
    5. Reviewing software development methodologies
      4m 8s
  2. 26m 14s
    1. Why we use object-orientation
      2m 42s
    2. What is an object?
      5m 22s
    3. What is a class?
      4m 43s
    4. What is abstraction?
      2m 45s
    5. What is encapsulation?
      3m 45s
    6. What is inheritance?
      3m 35s
    7. What is polymorphism?
      3m 22s
  3. 12m 16s
    1. Understanding the object-oriented analysis and design processes
      4m 13s
    2. Defining requirements
      6m 9s
    3. Introduction to the Unified Modeling Language (UML)
      1m 54s
  4. 23m 35s
    1. Understanding use cases
      6m 11s
    2. Identifying the actors
      4m 16s
    3. Identifying the scenarios
      5m 7s
    4. Diagramming use cases
      4m 18s
    5. Employing user stories
      3m 43s
  5. 16m 36s
    1. Creating a conceptual model
      1m 59s
    2. Identifying the classes
      2m 27s
    3. Identifying class relationships
      2m 38s
    4. Identifying class responsibilities
      6m 43s
    5. Using CRC cards
      2m 49s
  6. 22m 25s
    1. Creating class diagrams
      6m 11s
    2. Converting class diagrams to code
      4m 57s
    3. Exploring object lifetime
      5m 55s
    4. Using static or shared members
      5m 22s
  7. 19m 49s
    1. Identifying inheritance situations
      6m 49s
    2. Using inheritance
      2m 43s
    3. Using abstract classes
      2m 2s
    4. Using interfaces
      4m 20s
    5. Using aggregation and composition
      3m 55s
  8. 9m 23s
    1. Creating sequence diagrams
      5m 18s
    2. Working with advanced UML diagrams
      2m 3s
    3. Using UML tools
      2m 2s
  9. 10m 39s
    1. Introduction to design patterns
      2m 40s
    2. Example: the singleton pattern
      4m 53s
    3. Example: the memento pattern
      3m 6s
  10. 21m 47s
    1. Introduction to object-oriented design principles
      2m 50s
    2. Exploring general development principles
      3m 55s
    3. Introduction to SOLID principles
      6m 43s
    4. Introduction to GRASP principles
      8m 19s
  11. 7m 1s
    1. Reviewing feature support across different object-oriented languages
      3m 50s
    2. Additional resources
      2m 27s
    3. Goodbye
      44s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Foundations of Programming: Object-Oriented Design.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.