0% found this document useful (0 votes)
45 views9 pages

Advance Object Oriented Programming (Week 3) : Ms. Sehresh Khan Lecturer Numl-Rwp

The document discusses object-oriented programming concepts like composition, where one class contains an instance of another class as a component. It provides code examples of a Laptop class and a Student class, where the Student class contains a Laptop object as a composed member. The Main class instantiates a Student object, calls methods to set its properties including the composed Laptop, and then displays the Student details including the Laptop details.

Uploaded by

Shahzaib Aqeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views9 pages

Advance Object Oriented Programming (Week 3) : Ms. Sehresh Khan Lecturer Numl-Rwp

The document discusses object-oriented programming concepts like composition, where one class contains an instance of another class as a component. It provides code examples of a Laptop class and a Student class, where the Student class contains a Laptop object as a composed member. The Main class instantiates a Student object, calls methods to set its properties including the composed Laptop, and then displays the Student details including the Laptop details.

Uploaded by

Shahzaib Aqeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Advance Object Oriented Programming

(Week 3)

Ms. Sehresh Khan


Lecturer
NUML-RWP
Composition
Composition
 Has-a relationshipbetween objects
 Implemented using instance variable
 Code Reuse and More Organized
 Increase Encapsulation
Laptop Class
 import java.util.*; 
 public class Laptop {  public void set()
 String model;  {
 float RAM;  System.out.println("Enter Laptop Model:");
 int HD;  model=S.nextLine();
 Scanner S=new Scanner(System.in);   System.out.println("Enter Laptop RAM:");
 RAM=S.nextFloat();
 System.out.println("Enter Laptop HD:");
 public Laptop() {}
 public Laptop(String m, float r, int h)
 HD=S.nextInt();
 {
 }
 model=m;  }
 RAM= r;
 HD = h;
 }
 public void show()
 {
 System.out.println("Laptop Model: "+model);
 System.out.println("Laptop RAM: "+RAM);
 System.out.println("Laptop HD: "+HD);
 }
Student Class
 import java.util.*;   public  void set()

 public class Student {  {

 int id;  System.out.println("Enter Student Name:");

 String name;  name= S.nextLine();

 Laptop L;//composition  System.out.println("Enter Student ID:");

 Scanner S=new Scanner(System.in);   id=S.nextInt(); 

public Student()  System.out.println("Enter Student's Laptop Data:");

 {  L.set();

 id=0;  }

 name="";  }

L=new Laptop();
 }
 public  void show()
 {
 System.out.println("Student Name:"+name);
 System.out.println("Student ID:"+id);
 L.show();
 }
Main Class

 public class Main {

 public static void main(String[] args) {


 Student S=new Student();


 S.set();
 S.show();

 }
 }
Composition Task 1
Composition Task 2

You might also like