1 / 18

Structured Programming 1401104-3 Dr. Atif Alhejali

Structured Programming 1401104-3 Dr. Atif Alhejali. Lecture 3 Class Attributes Polymorphism Constructors. Global and local variables. In java; any variable that is defined in a block of code is only visible inside this block. Global and local variables. public class Test { int a;

metta
Download Presentation

Structured Programming 1401104-3 Dr. Atif Alhejali

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structured Programming1401104-3Dr. Atif Alhejali Lecture 3 Class Attributes Polymorphism Constructors Structured Programming

  2. Global and local variables • In java; any variable that is defined in a block of code is only visible inside this block. Structured Programming

  3. Global and local variables public class Test { int a; public void method1(int b) { intc; for(inti=0;i<10;i++){ intd; } } public void method2(int e){ int f; } } b and c visibility d visibility i visibility a visibility e and f visibility Structured Programming

  4. Global and local variables • A local variable is the variable that is defined within a method • A global variable is a variable that belongs to the class itself and hence it is defined within the class but not inside any method • In the previous example • variable a is a global variable to class Test • variables b and c are local variables to method1 • Variables i and d are local variables to the for loop in method1 • Variables e and f are local variables to method2 Structured Programming

  5. this • “this” is a keyword that is used in instance methods to refer to the object on which they are working • this is usually an immutable reference or pointer which refers to the current object Structured Programming

  6. this public class Student { long stdID; intnoOfCourses= 0; public void setstdID(long stdID){ this.stdID = stdID; } public void setstdID(intnoOfCourses){ this.noOfCourses= noOfCourses; } } Structured Programming

  7. this • In the previous example the word this referred to a virtual object from the type Student and hence any variable that is reached using “this.” is a global variable. These variables are written in blue (stdID) and purple (noOfCourses) • On the other hand the variables in red (stdID) and orange (noOfCourses)are local variables • This keywords makes it possible to distinguish between two variables with the same name by their position as global variables and local variables. Structured Programming

  8. Polymorphism • Polymorphism is a principle in biology were the same organism can has many forms. • In programming languages this principle can be applied to several parts of the code • The first and simplest type of polymorphism is the method polymorphism within the same class. Structured Programming

  9. Polymorphism • Any method on the class is identified by its • Name • list of arguments • return type. • The name is the main method identifier. • method polymorphism means that the method will have several versions with the same name and return type but with different arguments. Structured Programming

  10. Polymorphism • For example the following methods can exist at the same class. • public int Method1(){ … } • public int Method1(int x){ … } • public int Method1(int x, int y){ … } • public int Method1(double x){ … } • public int Method1(float x){ … } Structured Programming

  11. Polymorphism • On the other hand two methods cannot coexist on the same class if they have the same name and arguments and different return types • For example the two following methods will create a complier error. • public intMethod1(int x){ … } • public double Method1(int x){ … } Structured Programming

  12. Constructor In object-oriented programming, a constructor in a class is a special type of subroutine called to create an object. It prepares the new object for use, often accepting parameters that the constructor uses to set member variables required Structured Programming

  13. Constructor Car car = new Car () ; Public Class Car{ public Car() { … … … } … … … } Constructor calling Constructor Structured Programming

  14. Constructor • The constructor is a special method that: • Does not have a return value or return value type. • Must have the exact same name as the class • May or may not have arguments. • If the class does not have a constructor then a default empty constructor will be added to it. • The constructor will be called each time a new object is created which is every time the key word newis used. • The same class can have more than one constructor all with the same name but with different arguments. Structured Programming

  15. Constructor Public class Example1{ int x; int y; public Example1(){ } } Example1 ex1 = new Example1(); Structured Programming

  16. Constructor Public class Example1{ int x; int y; public Example1(){ x = 0; y = 1; } } Example1 ex1 = new Example1(); Structured Programming

  17. Constructor Public class Example1{ int x; int y; public Example1(int a, int b){ x = a; y = b; } } Example1 ex1 = new Example1(0,1); Structured Programming

  18. Constructor Public class Example1{ int x; int y; public Example1(){ x = 0; y = 1; } public Example1(int a, int b){ x = a; y = b; } } Example1 ex1 = new Example1(); Example1 ex2 = new Example1(0,1); Structured Programming

More Related