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

Exploring object lifetime

From: Foundations of Programming: Object-Oriented Design

Video: Exploring object lifetime

One place that is worth talking about language differences is in the idea of object lifetime. How are objects created? What happens when they're created? And what happens when with them? We know the core idea is that once we define a class, we make an instance of it. We make an object so we can use the object, and this is instantiation, creating an instance. To create an object, most object- oriented languages use the word new. I'm going to show a few examples here. Don't worry about syntax, just take a look at the different languages.

Exploring object lifetime

One place that is worth talking about language differences is in the idea of object lifetime. How are objects created? What happens when they're created? And what happens when with them? We know the core idea is that once we define a class, we make an instance of it. We make an object so we can use the object, and this is instantiation, creating an instance. To create an object, most object- oriented languages use the word new. I'm going to show a few examples here. Don't worry about syntax, just take a look at the different languages.

So I have a Customer class, I want to make a Customer object. In Java, I'd say Customer fred = new Customer. It would be identical in C#, also using the word new. In VB.NET, we'd have something fairly similar, again, using the word new, as does Ruby. C++ looks very similar to Java, and C# except it's using the asterisk for a pointer symbol. And Objective-C likes to be different, so it's using alloc and init instead of new. Objective-C actually does have a new keyword, it's just more common to see this format.

But, as you can see, most of the time it's the new keyword that is responsible for creating an object. In the background, the computer is allocating a little bit of memory for your new object, and then initializing all the variables in it and returning a reference to that object. Then you start to use it, and most of that happens without you needing to think about it. But your question is do you want something different to happen is created? Do you want to be part of that instantiation process? In the usual way, you can take part in instantiation is with something called a constructor.

Now if you're coming from a procedural language background like traditional COBOL, straight C, or Fortran, you don't have this idea. A constructor is an object orientation concept. Constructor is a special method that exists to construct the object. It will be called when the object is created. A little while ago I showed the idea of a basic Spaceship class, and now I want to make a Spaceship object. I'm going to use typical C# or Java format. So I've created using the word new. And the question is what is the internal state of that object right now? Well, I know that the class defined a String and an Integer, those were my instance variables.

So, the object has been created, and it has two variables, name, and shieldStrength. The name is null and shieldStrength, which was an integer, is 0. The object exists, but it has a meaningless state. Now, I could start setting its values. But what if I wanted to create it in a meaningful state to begin with? Well, we can provide constructor methods to do this. Basically, a constructor can make sure that any variables belonging to that object are immediately set to the right values as soon as the object is created.

In Java or C#, or C++, what you do in the class is you create a constructor by simply creating a method in the class with the same name as the class. Now, there's no return type to it, because you'd never call it yourself. It's called when you use the word new. So, in this case, I would create a method with the same name as the class, and in it, I'm going to set the initial state that I want these instance variables to be. I then use the same code to instantiate that object, but it will now be instantiated with some actual values in those instance variables.

So, this is a basic constructor that takes no arguments, and in a UML class diagram, if you see a method with the same name as the a constructor. Now, in VB.NET and Python, constructors are created by making a new method. The actual method has the name new. In Ruby, it's by creating a method called initialize, and in Objective-C, it's by creating an init method in the class. Now, in most languages, we can create multiple constructors, what's called overloading or constructor methods.

So, if I went back to that class definition, added a second method, it's also called Spaceship, same name as the class, but this one takes a parameter just one string which I've called n. And now when I instantiate that object, I have two ways of doing it, I can use the word new with no parameters, or I can use the word new along with a string parameter. And in this case, it's going to call the overloaded constructor which is immediately going to set the values of those instance variables to different values.

So, overloaded constructors allows me to have flexibility and pass-in information when actually creating the object, and that in UML would typically be represented something like this. You would see two methods with the same name as the class, one with no parameters, and one with parameters. And this is a very simple example. When you have more complex object, it's often very important to make sure they aren't instantiated in an invalid state, particularly when one object needs to contain other objects and often has to create them as part of that instantiation process.

All languages can get deeper on the idea of constructors or initializers, but I'll leave that for you to explore. Now, on the other side of the lifetime equation, we also have the idea of a destructor. This is a method that is called when an object is no longer needed and is being disposed off. In some languages, this role is played by something called a finalizer rather than a destructor, but the concept is much the same, a place to put some code that will automatically be called when the object is destroyed. Now, destructors are not needed as often as constructors, but they do have their uses.

They're typically used if you have an object that is holding a resource, say it has a document open on the file system or it's connected to a database, and you just want to make sure that, that object has released any connection that it has before it's destroyed.

Show transcript

This video is part of

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

47 video lessons · 48101 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 preferencesfrom 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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.