Lecture-6 - Object Oriented Programming Instructor Name...

This preview shows page 1 - 9 out of 29 pages.

Object Oriented Programming Lecture-6 Instructor Name:
Image of page 1

Subscribe to view the full document.

Today’s Lecture Components of Class Fields Methods Main() Method, Setter Method, Getter Method, immutator. Calling Method in same class Main method Calling Method in other class. Creating Objects from class 2
Image of page 2
Class & Methods Keyword - this Can be used only inside method When call a method within the same class, don’t need to use this , compiler do it for you. When to use it? method parameter or local variable in a method has the same name as one of the fields of the class Used in the return statement when want to return the reference to the current object. 3
Image of page 3

Subscribe to view the full document.

Class & Methods Keyword – this Example 1 class A{ int w ; public void setValue (int w ) { this . w = w ; //same name! When a method parameter or local variable in a method has the same name as one of the fields of the class, you must use this to refer to the field. 4 } }
Image of page 4
Class & Methods Keyword – this Example 2 class Exp{ public int i=0; public Exp increment () { i++; return this ; // return current object 5 } public static void main (String[] args){ Exp e = new Exp(); int v = e.increment().increment().i; // v=2!! } }
Image of page 5

Subscribe to view the full document.

Object Initialization When objects are created, the initial value of data fields is unknown unless its users explicitly do so. For example, ObjectName.DataField1 = 0; // OR ObjectName.SetDataField1(0); In many cases, it makes sense if this initialisation can be carried out by default without the users explicitly initializing them. For example, if you create an object of the class called “Counter”, it is natural to assume that the counter record-keeping field is initialized to zero unless otherwise specified differently. 6 class Counter { int CounterIndex; } Counter counter1 = new Counter(); What is the value of “counter1.CounterIndex” ? In Java, this can be achieved though a mechanism called constructors.
Image of page 6
Class & Constructor What is a Constructor? A special method automatically called when an object is created by new() Java provide a default one that takes no arguments and perform no special initialization Initialization is guaranteed All fields set to default values: primitive types to 0 and false, reference to null 7
Image of page 7

Subscribe to view the full document.

Class & Constructor What is a Constructor? Constructor in java is a special type of method that is used to initialize the object .
Image of page 8
Image of page 9

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern