--- description: In this lab, we are going to talk about object-oriented programming, which is not something that tells the computer to do better or new instructions, it's just a way to organize the data and functionality in your code. --- <h1 style='border: none'><center>Programming I Lab 11</center></h1> <h2 style='border: none'><center>Introduction to Object-Oriented Programming</center></h2> <h5><center>The Islamic University of Gaza<br>Engineering Faculty<br>Department of Computer Engineering</center></h5> <h6>Author: Mohammed Nafiz ALMadhoun<span style="float:right">2021/12/10</span></h6> --- <p style='text-align:justify'> In this lab, we are going to talk about object-oriented programming, which is not something that tells the computer to do better or new instructions, it's just a way to organize the data and functionality in your code. </p> ## Defining Classes The main idea of OOP is that we describe stuff as in real life, each object in real life has a type or class (e.g. you're a human), and we describe the thing according to what we are talking or thinking about, so you're a student if we describe you in the university and you're also a human if we describe you in the animal kingdom. To define a class in Java, we could write: ```java= class Student { } ``` Describing a class without properties is kinda useless, so we could add some properties to this class such as data fields. ```java= class Student { public String name; public int birthYear; public double gpa; } ``` We could also add methods to this class, which represent an action for this class: ```java= class Student { public String name; public int birthYear; public double gpa; public void printName () { System.out.println(name); } } ``` Notice that we didn't write `static`! **Note:** We write `public` before the field type to access it from outside of the class, later we will learn about setters and getters. ## Creating Objects The object is an instance of the class, in real life, you're a student and your friend is a student and so on, previously we created the type itself, but now we will create the real object: ```java= public static void main(String[] args){ Student s1 = new Student(); s1.name = "Mohammed"; s1.birthYear = 1997; s1.gpa = 100; s1.printName(); // This will print `Mohammed` } ``` As we've created instances of `Scanner` in the previous labs, we will do the same with our custom class, we will create a student and store it in the variable `s1`. We could access the public data field simply by writing its name, and we could call the public methods as we've called methods in different stuff (e.g. input.nextInt()). Notice that we've created a new type now, which has its properties. <div style="page-break-after: always;"></div> ### Constructors As you may notice, when we create a new Scanner, we give it the source that it reads from (e.g `System.in`, or a `File`). The constructor will allow us to write a code that will be executed when we created a new instance of that class: ```java= class Student { public String name; public int birthYear; public double gpa; public Student (String name, int birthYear, double gpa) { this.name = name; this.birthYear = birthYear; this.gpa = gpa; System.out.println ("Student "+name+" Created!"); } } ``` The constructor is just like a method, but it has no return type (as it will return the class type!). So, if you want to create a student, you could write: ```java= public static void main(String[] args) { Student s1 = new Student ("Mohammed", 1997, 100); } ``` ## Final Words! This lab is just a brief introduction to object-oriented programming, in the next semester, we will learn more about it. Hope you enjoyed this semester lab and enjoyed programming, with the knowledge you have right now, you can program many things, so try to learn how to create other applications using Java or any other programming language. Good luck with your final exams <3. ###### tags: `Programming` `Java` `IUG` <center>End Of Lab 11</center>