From the course: Programming Foundations: Object-Oriented Design
Unlock the full course today
Join today to access over 24,400 courses taught by industry experts.
Class with multiple constructors - Python Tutorial
From the course: Programming Foundations: Object-Oriented Design
Class with multiple constructors
- We've defined a constructor to instantiate our spaceship object, and it sets the callsign attribute to a value of the nameless ship. But, what if I don't want all of the ships we instantiate to have a callsign of the nameless ship? That'd get confusing. - Well, most languages will let us create multiple constructor methods through a process called overloading, which allows a class to have more than one method with the same name, but different sets of input parameters. To do that in Java, we'll create a second method in this spaceship class, also called spaceship, but this one takes a parameter. Just a string, which I've called name and that gets assigned to the callsign variable. Now when we instantiate a spaceship object, we have two ways of doing it. We can use the word new with no parameters, like before, which will call the first constructor method to give us the nameless ship, or, we can use the word new, along…
Contents
-
-
-
-
-
-
-
(Locked)
Creating class diagrams: Attributes2m 23s
-
(Locked)
Creating class diagrams: Behaviors3m 9s
-
(Locked)
Converting class diagrams into code4m 29s
-
(Locked)
Instantiating classes3m 38s
-
(Locked)
Class with multiple constructors2m 29s
-
(Locked)
Static attributes and methods4m 19s
-
(Locked)
Challenge: Jukebox class diagrams1m 18s
-
(Locked)
Solution: Jukebox class diagrams5m 5s
-
(Locked)
-
-
-
-