introduction to object oriented programming and classes of programming i - lecture 09.pdf · the...
TRANSCRIPT
Introduction to Object Oriented Programming and classes
Dr. Ahmed ElShafee
١ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
Agenda
• Introduction• MyFirst class• Parameters Local scope and global scope• The Class Constructor• Accessing Class Values• Inheritance• Understanding Instance and Class Members• Constants
٢ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
Introduction
• the difference between an object and class, though, when we talk about a class we’re talking about the code itself, the code that sits around doing nothing.
• When you put the code to work it’s an object.• When you create a class, you’re writing code to do a particular
job. • That job might be to do with an employee,
٣ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
MyFirst class
• Create a new project for this. • Call the package exams, and then change the name of the
Main method to ExamDetails. • You should then have the following code:
٤ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• We’ll create a second class to handle the exam data. • So, in NetBeans, click File from the menu bar at the top. From
the File menu, select New File. • Highlight Java from the Categories list, and Java Class from
the File Types list. • Then click Next. On the next screen, enter StudentResults as
the class name. • Then click Finish. • NetBeans will create a second class in your project. • You can delete any default comments.
٥ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
exams01
•
٦ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
•
٧ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
•
٨ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
Parameters Local scope and global scope
• In the previous lecture, we talked about variables inside of methods.
• The variables you set up inside of methods are only available to those methods.
• They are not available to other methods. • They are said to have local scope.• However, you can set up variables outside of methods that all
the methods in your class can see. • These are known as Field variables, or just Fields. • You set them up in exactly the same way as any other
variable. • Add the following four fields to your new StudentResults
class:٩ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• These four fields will be available to all the methods that we write in this class, and won’t be local to any one method.
• They are said to have global scope.
١٠ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• Add the following line of code to create a new object from• your StudentResults class:
• This is the same as we did in the previous section – used the new keyword to create a new object.
• The name of the object will be aStudent, and it is of type StudentResults, which is our class١١ Dr. Ahmed ElShafee, fundamentals of Programming I,
ACU/CSIT Spring 2012
• On the next line, type the name of the variable (aStudent), followed by a dot.
• As soon as you type the dot NetBeans display a popup list of methods and properties available to your object:
١٢ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• The four fields that we set up are on the list. They are not methods, but something called a property.
• The fact that they are on the list at all means that they have global scope.
• If they had local scope they wouldn’t be on the list.• Add the following code
١٣ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
exams01
•
١٤ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
•
١٥ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• However, it’s not a good idea to make field variables global like this.
• You tend to lose track of just what values are being held in them, and it therefore makes debugging your code a lot harder.
• It’s considered good coding to narrow the scope of field variables.
١٦ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• Now, only code within the StudentResults class can see these field variables.
• To check, go back to your main method. You should see some warnings and red underlines:
• Delete the three lines at the bottom. • Type aStudent and then a dot again to see the NetBeans
popup list:١٧ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• As you can see, the four field variables have now vanished.
١٨ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
The Class Constructor
• When the object is created, Java calls the constructor first. Any code you have in your constructor will then get executed.
• You don’t need to make any special calls to a constructor method – they happen automatically when you create a new object.
• Constructor methods take the same name as the class.
١٩ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• class constructors don’t need a return type like int or double, nor any return value.
• You can, however, pass values over to your constructors. If we want to pass values over to our field variables, we can do this:
٢٠ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• When you create a new object, you’d then need two strings• between the round brackets of the class name:StudentResults aStudent = new StudentResults( "Bill Gates",
"A" );• When the object is created, the values “Bill Gates” and “A”
will be handed over to the constructor.• However, it’s a good idea to just set some default values for
your field variables.
٢١ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
٢٢ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• All four of our field variables will now have default values whenever a new StudentResults object is created.
Exams02
•
٢٣ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
•
٢٤ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
Accessing Class Values
• Add the following method to your StudentResults class:
٢٥ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• Click back to your ExamDetails class. Add the following two lines:
String sName = aStudent.fullName("Bill Gates");System.out.println( sName );
• Just like the methods we created in the previous section, our fullNamemethod is on the NetBeans popup list.
• Notice that the constructor is not there,
٢٦ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• Run your code to test it out.
٢٧ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
Exams03
•
٢٨ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
•
٢٩ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• What we’ll do is to allow a user to enter a two letter exam code.
• We’ll then pass those two letters over to a method that turns the two letters into an exam name.
• For example, if a user enters the letter “VB”, the method will return the string “Visual Basic .NET”.
• The longer string will be stored in the Exam_Name field variable.
٣٠ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• Go back to your ExamDetails class and add the following line:String exam = aStudent.examName("VB");
٣١ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
Exams04
•
٣٢ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
•
٣٣ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
•
٣٤ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
•
٣٥ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• Add the a method to calculate the above grades (add it to your StudentResults class):
• Notice that this method is private. • Just like field variables, making a method private means it can
only be seen inside of this class. • It can’t be seen by the ExamDetails class.• To get the grade, we’ll set up another method inside of the
StudentResults class.• We’ll use this to get the grade. Add the following method just
above the getGrademethodString examGrade(int aScore) {
Exam_Grade = this.getGrade( aScore) ;return Exam_Grade;}٣٦ Dr. Ahmed ElShafee, fundamentals of Programming I,
ACU/CSIT Spring 2012
Notes:• Calling one method from another is standard practice, and it
allows you to simplify your code. • The alternative is to have very long methods that are hard to
read.• The this keyword means “this class”, rather than another class
that may have the same, It avoids any confusion. It’s strictly not necessary, and we could have left it out.
• The method call would still work without it:Exam_Grade = getGrade( aScore );
٣٧ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
Exam05
•
٣٨ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
•
٣٩ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
•
٤٠ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
•
٤١ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
•
٤٢ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
•
٤٣ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
Inheritance
• But essentially, it’s having one class as a parent class (called a super class) and another class as a child of the parent (the sub class).
• The child class is said to be derived from the parent class. The reason to have a child class, though, is to keep information separate.
• The child can inherit all the methods and fields from its parent, but can then do its own thing.
٤٤ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• As an example of inheritance, we’ll create a sub class that handles information about certificates.
• If the student gets an “A” grade, we’ll award a Certificate of Excellence; if the student gets a “B” or a “C” we’ll award a Certificate of Achievement.
• Any other grade and no certificate will be awarded. • But the point about the sub class is to keep the certificates
data separate from the exam data.• However, we may still want to access some of the information
about the exam, such as which exam it was. • We can even access the methods that turn a score into a
grade, and all from the sub class.٤٥ Dr. Ahmed ElShafee, fundamentals of Programming I,
ACU/CSIT Spring 2012
• So, create a new class by clicking File > New File from the NetBeans menu.
• When the dialogue box appears, select Java under the Categories heading, and Java Class under File Types.
• Click Next, and enter Certificates as the name of your new class.
• Click Finish.• add a private String field and call it certificate.• Your new class should then look like this
٤٦ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012
• To create a sub class (child) from a super class (parent), the keyword extends is used.
• You then follow the “extends” keyword with the parent class you want to extend.
• After “public class Certificates” in your code add “extends StudentResults”.
٤٧ Dr. Ahmed ElShafee, fundamentals of Programming I, ACU/CSIT Spring 2012