Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
Learn composition in 6 minutes! ⚙️
Play lesson

Java tutorial for beginners ☕ - Learn composition in 6 minutes! ⚙️

5.0 (4)
40 learners

What you'll learn

This course includes

  • 12 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

#java #javatutorial #javacourse public class Main { public static void main(String[] args) { // Composition = Represents a "part-of" relationship between objects. // For example, an Engine is "part of" a Car. // Allows complex objects to be constructed from smaller objects. Car car = new Car("Corvette", 2025, "V8"); System.out.println(car.model); System.out.println(car.year); System.out.println(car.engine.type); car.start(); } } public class Car { String model; int year; Engine engine; Car(String model, int year, String engineType){ this.model = model; this.year = year; this.engine = new Engine(engineType); } void start(){ this.engine.start(); System.out.println("The " + this.model + " is running"); } } public class Engine { String type; Engine(String type){ this.type = type; } void start(){ System.out.println("You start the " + this.type + " engine"); } }

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere