1 / 35

BBIT 212/ CISY 111 Object Oriented Programming (OOP)

BBIT 212/ CISY 111 Object Oriented Programming (OOP). Objects and classes, object interactions in Java 22.02.2011. (General!) Recap. The two steps of Object Oriented Programming Making Classes: Creating, extending or reusing abstract data types.

konala
Download Presentation

BBIT 212/ CISY 111 Object Oriented Programming (OOP)

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. BBIT 212/ CISY 111 Object Oriented Programming (OOP) Objects and classes, object interactions inJava 22.02.2011

  2. (General!) Recap • The two steps of Object Oriented Programming • Making Classes: Creating, extending or reusing abstract data types. • Making Objects interact: Creating objects from abstract data types and defining their relationships.

  3. 22ndFeb 2011 - RECAP • Anatomy of a Java Program • Comments • Reserved words • Modifiers • Statements • Blocks • Classes • Methods • The main method

  4. Recap • From the notes! • Variables • What they are? • Declaring • Initializing * • Operators • Numeric operators • Shorthand operators • Increment and Decrement operators • Constants • Expressions, Statements and Blocks

  5. Variables • A variable is a “place-holder” (in memory) representing data of a certain type • Variable declaration tells the compiler “the name of the variable as well as what type of data it represents” datatypevariableName; • E.g. • int radius; • double pie; • If variables are of the same type, they can be declared together, as follows: datatype variableName1, variableName2, ..., variableNameX; E.g: double pie, perimeter, area;

  6. Declaring and initializing variables in one step • Variables often have initial values. You can declare a variable and initialize it in one step. • intradius = 7; • This is equivalent to the next two statements: intradius; radius = 1; • You can also use a shorthand form to declare and initialize variables of the same type together. For example, inti = 1, j = 2;

  7. (Numeric) Operators

  8. Shorthand Operators • Very often the current value of a variable is used, modified, and then reassigned back to the same variable. • E.g.: i = i + 8; • Java allows you to combine assignment and addition operators using a shorthand operator. For example, the example statement can be written as: i += 8;

  9. Shorthand Operators (2)

  10. There are two more shorthand operators for incrementing and decrementing a variable by 1. This is handy because that is often how much the value needs to be changed.

  11. Variable types • A variable's data type determines the values it may contain, plus the operations that may be performed on it • A primitive type is predefined by the language and is named by a reserved keyword. Java has eight such: • Byte, short,int, long, float, double,boolean, char • REF: Primitive types in Java.pdf

  12. Today • Objects and classes • Object interactions • Others • A sample form of the exercise from last week + introduce Flow control • CAT next week

  13. Recap • Exercise (in groups!) • Creating a table (as reference) to primary school students for Perimeter and Area of a Circle

  14. What you should know by now! • The anatomy of a class • The main method (signature, use) • Variables declaration and use

  15. Today • Objects and classes • Object interactions • Others • A sample form of the exercise from last week + introduce Flow control • CAT next week

  16. Are objects same as classes?

  17. Are objects same as classes? • NO! • A class is a data structure (like a variable definition) that will be used later on • An object is an instance (a specific use of a class)

  18. E.g. • A person class • Defines some properties that ANY person can have name, date of birth, identification, etc • For a specific person (e.g. named “Mary” or “Tom”), we need to create an instance of a Person and set values of person’s name, date of birth, identification, etc accordingly • A Person Object (say Mary) • Mary, 01/01/80, 144

  19. public class Person{ String name; String dateOfBirth; int identification; public static void main (String args []) { System.out.println ("This is a Peron Class"); } }

  20. public class Person{ String name; String dateOfBirth; int identification; // a method to print details of a person public void printDetails(){ System.out.println("Name is:" + name); System.out.println(" Born: " + dateOfBirth); System.out.println(" ID Number: " + identification ); } public static void main (String args[]) { System.out.println ("This is a Peron Class"); } }

  21. How are objects created? • A class constructor is used to create instances of a class by using the new keyword • Recall

  22. public class Person { String name; String dateOfBirth; int identification; // a method to print details of a person public void printDetails(){ System.out.println("Name is:" + name); System.out.println(" Born: " + dateOfBirth); System.out.println(" ID Number: " + identification ); } //Constructor a Person class that receives the person details Person(String a,String b, int c ) { name = a; dateOfBirth = b; identification = c; } }

  23. public class Person { String name; String dateOfBirth; int identification; // a method to print details of a person public void printDetails(){ System.out.println("Name is:" + name); System.out.println(" Born: " + dateOfBirth); System.out.println(" ID Number: " + identification ); } //Constructor a Person class that receives the person details Person(String a,String b, int c ) { name = a; dateOfBirth = b; identification = c; } public static void main (String args[]) { //using the constructor to create an instance of a Person Person mary = new Person("Mary", "01/01/90",144); mary.printDetails(); } }

  24. Demo

  25. Class constructor • Constructor name is class name. A constructors must have the same name as the class its in • Default constructor. If you don't define a constructor for a class, a default parameterless constructor is automatically created by the compiler. The default constructor calls the default parent constructor (super() from the Object class) and initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans).

  26. Default constructor is created only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created. • Differences between methods and constructors. There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value. • There is no return statement in the body of the constructor. • The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.

  27. Today • Objects and classes • Object interactions • Others • A sample form of the exercise from last week + introduce Flow control • CAT next week

  28. We will create a sub class Student in the Person class

  29. Today • Objects and classes • Object interactions • Others • A sample form of the exercise from last week + introduce Flow control • CAT next week

  30. Practice !

More Related