Skip to content

Instantly share code, notes, and snippets.

@arttuladhar
Created July 16, 2018 22:38
Show Gist options
  • Save arttuladhar/994403cd6d9636f276c2b0f5876989d1 to your computer and use it in GitHub Desktop.
Save arttuladhar/994403cd6d9636f276c2b0f5876989d1 to your computer and use it in GitHub Desktop.
Composition Demo
package com.art.fundamentals;
public class CompositionDemo {
public static void main(String[] args) {
Question firstQuestion = new Question("Who won 2018 World Cup ?", "France", "Croatia");
Question secondQuestion = new Question("Who hosted 2018 World Cup ?", "Russia", "USA");
System.out.println(firstQuestion);
System.out.println(secondQuestion);
}
}
class Question {
private String question;
private Option optionOne;
private Option optionTwo;
Question(String question, String option1, String option2) {
this.question = question;
this.optionOne = new Option(1, option1);
this.optionTwo = new Option(2, option2);
}
@Override
public String toString() {
return "Question{" +
"question='" + question + '\'' +
", optionOne=" + optionOne +
", optionTwo=" + optionTwo +
'}';
}
}
class Option{
int id;
String name;
Option(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Option{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment