Lecture 2 - Object Oriented Programming PDF

Summary

This document provides a lecture on object-oriented programming concepts, focusing on class structure, methods, and attributes. It includes examples illustrating the creation and use of Java classes and objects. It contains details on the importance of data hiding and object oriented programming features. It is likely a set of course lecture notes.

Full Transcript

Lecture 2 Object Oriented Programming Dr.Shaymaa E. Sorour Student 1 Student 2 Student 3 Data Data 1. Student Name 1. Student Name Data 2. University ID 2. University ID 1. Student Name 3....

Lecture 2 Object Oriented Programming Dr.Shaymaa E. Sorour Student 1 Student 2 Student 3 Data Data 1. Student Name 1. Student Name Data 2. University ID 2. University ID 1. Student Name 3. Birth Date 3. Birth Date 2. University ID 4. Address 4. Address 5. GPA 5. GPA 6. Study Level 6. Study Level 6. Study Level Operations Operations Operations 1. Modify GPA() 1. Modify GPA() 1. Modify GPA() 2. Change Level() 2. Change Level() 2. Change Level() 3. Set Name() 3. Set Name() 4. Set Address() 4. Set Address() 4. Set Address() Student 1 Class Student Objects Data 1. Student Name Data 2. 3. University ID Birth Date Student 2 4. Address 1. Student Name 5. GPA Data 6. Study Level 2. University ID 7. Email 1. Student Name 2. University ID 3. Birth Date 3. Birth Date Student 3 4. Address Operations 4. Address 1. Modify GPA() 5. GPA 5. GPA 2. Change Level() 6. Study Level Data 7. Email 6. Study Level NEW 3. Set Name() 1. 2. Student Name University ID 7. Email 4. Set Address() Operations 3. Birth Date 5. Print Student Info() 4. Address 1. Modify GPA() 5. GPA 2. Change Level() 6. Study Level 3. Set Name() 7. Email Operations 4. Set Address() 1. Modify GPA() 5. Print Student Info() Operations 1. Modify GPA() 2. Change Level() 2. Change Level() 3. Set Name() 3. Set Name() 4. Set Address() 4. Set Address() 5. Print Student Info() 5. Print Student Info() Create Your First Class in Java public class student { student myS = new student(); public int ID; myS.ID = 1; public int University_ID; myS.Address = "Kafr El-Shiekh"; public double GPA; myS.GPA = 2.5; public String Address; myS.University_ID = 14; } Student myS ID ID 1 University_ID University_ID 14 GPA GPA 2.5 Address Address Kafr El-Shiekh Class Object "Kafr El-Shiekh" true public class rectangle { Rectangle private double length; private double width; } Length Private public Data item width Attributes When the private access When the public modifier is applied to a access modifier is class member, the applied to a class setLength() member cannot be member, the member setWidth() accessed by code outside can be accessed by getLength() the class. The member code inside the class getWidth() can be accessed only by or outside. getArea() methods that are members of the same class. Data Hiding An object hides its internal, private fields from code that is outside the class that the object is an instance of. Only the class's methods may directly access and change the object's internal data. Code outside the class must use the class's public methods to operate on an object's private fields. Data hiding is important because classes are typically used as components in large software systems, involving a team of programmers. Data hiding helps enforce the integrity of an object's internal public class rectangle { public static void main(String[] args) { private double length; rectangle r1 = new rectangle(); private double width; r1.length = 5; public void setLength(double L) r1.setLength(5); { double a = r1.getArea(); length = L; } } public void setWidth(double W) { Width = W; } public double getLength() { return length; } Length has private access in rectangle public double getwidth() { return width; } public double getArea() { return width * length; } public static void main(String[] args) { rectangle r1; rectangle r1 = new rectangle(); } Uninitialized Local Reference Variables Reference variables can be declared without being initialized. This statement does not create a Rectangle object, so it is an uninitialized local reference variable. A local reference variable must reference an object before it can be used, otherwise a compiler error will occur Constructor Constructor Classes can have special methods called constructors. A constructor is a method that is automatically called when an object is created. Constructors are used to perform operations at the time an object is created. Constructors typically initialize instance fields and perform other object initialization tasks. Constructor Constructors have a few special properties that set them apart from normal methods. ✓Constructors have the same name as the class. ✓Constructors have no return type (not even void). Integer/ ✓Constructors may not return any values. ✓Constructors are typically public. Overloading Methods and Constructors Two or more methods in a class may have the same name as long as their parameter lists are different. When this occurs, it is called method overloading. This also applies to constructors. Method overloading is important because sometimes you need several different ways to perform the same operation. public class rectangle { public static void main(String[] args) { private double length; rectangle r1 = new rectangle(); private double width; double a = r1.getArea(); System.out.println(a); } public rectangle() { length = 10; width = 15; initial } } public class rectangle { public static void main(String[] args) { private double length; rectangle r1 = new rectangle(); private double width; double a1 = r1.getArea(); System.out.print(a1); public rectangle() Rectangle r2 = new rectangle(3, 7); { double a2 = r2.getArea(); System.out.print(a2); length = 10; } width = 15; } public rectangle(double W, double L) { Length = L; Width = W; } } public class Employee { Class Employee private int Emp_ID; private String Name; private String Depart; private double Salary; private double bonus; Emp_ID :int private Boolean Resident; Name: String Depart: String public Employee() Salary: Double { Bonus: Double Emp_ID = 10; Resident: Boolean Name = “No Name”; Depart = “Not Assigned Yet”; Salary = 3000; Bonus = 500; Employee() Resident = true; Employee(int, String) } Employee(int, String, bool) Employee(int, String, String, double, double, bool) public Employee(int id, String name) Set_Salary(double): Void { Set_Salary(double, double): Void Emp_ID = ID; Set_depart(String): Void Name = name; Set_Emp_ID(int): Void } Print_Emp_Data(): Void public static void main(String[] args) { Employee emp1 = new Employee(); Employee emp2 = new Employee(101, “Ahmed”); } emp1 emp2 Emp_ID 100 Emp_ID 101 Name “No Name” Name “Ahmed” Depart “Not Assigned Yet” Depart NULL Salary 3000 Salary NULL Bonus 500 Bonus NULL Resident true Resident NULL public class Employee { Class Employee public Employee(int id, String name, boolean r) { Emp_ID = id; Name = name; Emp_ID :int Resident = r; Name: String } Depart: String } Salary: Double Bonus: Double public static void main(String[] args) { Resident: Boolean Employee emp1 = new Employee(102, “Amr”, true); } emp3 Employee() Emp_ID 102 Employee(int, String) Employee(int, String, bool) Name “Ahmed” Employee(int, String, String, double, double, bool) Depart NULL Set_Salary(double): Void Salary NULL Set_Salary(double, double): Void Bonus NULL Set_depart(String): Void Set_Emp_ID(int): Void Resident true Print_Emp_Data(): Void Static Class Members Static fields and static methods do not belong to a single instance of a class. To invoke a static method or use a static field, the class name, rather than the instance name, is used. Example: double val = Math.sqrt(25.0); Class Name Static Method class car { public static void main(String[] args) { car c1 = new car(); public int count; car c2 = new car(); public car() { car c3 = new car(); car c4 = new car(); count++; } double count = c4.count(); } } class car { public static void main(String[] args) { public static int count; car c1 = new car(); public car() { car c2 = new car(); car c3 = new car(); count++; car c4 = new car(); } } double count = car.count(); } Static Methods Static methods are convenient because they may be called at the class level. They are typically used to create utility classes, such as the Math class in the Java Standard Library. Static methods may not communicate with instance fields, only static fields.

Use Quizgecko on...
Browser
Browser