-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass Object and Overloaded Constructors
- Loading branch information
1 parent
3eb59b5
commit 39add6c
Showing
4 changed files
with
26 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package Objects; | ||
public class Garage { | ||
|
||
public Garage(){} | ||
|
||
public void Park(Car car/*Since car is inside the same package of Garage, which is Objects, we can use it inside this object without the need to import the package*/){ | ||
System.out.println("The "+ car.name +" is parked in the Garage"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
package files; | ||
import Objects.Car; | ||
import Objects.Garage; | ||
public class PassObject { | ||
public static void main(String[] args) { | ||
run(); | ||
} | ||
//How to pass objects as arguments? | ||
public static void run() { | ||
Garage garage = new Garage(); | ||
Car car1 = new Car("BMW"); | ||
Car car2 = new Car("Tesla"); | ||
garage.Park(car1); | ||
garage.Park(car2); | ||
} | ||
} |