180 likes | 321 Views
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;
E N D
Structured Programming1401104-3Dr. Atif Alhejali Lecture 3 Class Attributes Polymorphism Constructors Structured Programming
Global and local variables • In java; any variable that is defined in a block of code is only visible inside this block. Structured Programming
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
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
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
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
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
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
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
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
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
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
Constructor Car car = new Car () ; Public Class Car{ public Car() { … … … } … … … } Constructor calling Constructor Structured Programming
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
Constructor Public class Example1{ int x; int y; public Example1(){ } } Example1 ex1 = new Example1(); Structured Programming
Constructor Public class Example1{ int x; int y; public Example1(){ x = 0; y = 1; } } Example1 ex1 = new Example1(); Structured Programming
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
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