Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constructors A constructor is a method that has the same name as the class itself It is automatically called when an object is instantiated (in other words,

Similar presentations


Presentation on theme: "Constructors A constructor is a method that has the same name as the class itself It is automatically called when an object is instantiated (in other words,"— Presentation transcript:

1 Constructors A constructor is a method that has the same name as the class itself It is automatically called when an object is instantiated (in other words, when you “connect to a class”) example: Param Q = new Param( ); It is used to “set up” the class – usually this means initializing its instance variables, sometimes this means calling other methods Constructors cannot return a value (but they don’t use void either!) They cannot be called like other methods: they are only called when an object is instantiated Example: ConstructorDemoClass & ConstructorDemoClient

2 Encapsulation Encapsulation is an important concept in object- oriented programming. Essentially, it means that the internal workings of an object should be hidden. Or, in other words, when you (as a programmer) use a class (by creating an object), you should know what the class does, but not necessarily how it does it. When data is restricted by using private variables and methods, this is a concept called information hiding.

3 Practically speaking, this means simply that almost always, instance variables in a class should be private. Then, if you want to access those variables from a client, the only way to do this is by calling public methods which return those variables So, directly accessing a class’s variables violates the concept of encapsulation.

4 Encapsulation also means that even some methods should be private. This is because certain “helper” methods perform tasks that support other methods, and are not meant to be called by a client. Using helper methods is a concept known as procedural abstraction. Demo: EncapsulationDemoClass & EncapsulationDemoClient

5 toString( ) method Almost every class should have a toString( ) method. toString() is used to quickly and easily display the most important info from a class toString( ) is called automatically when you put the name of an object inside a System.out.println( ). So, you do NOT call toString( ) the same way you call other methods! Demo: add on to EncapsulationDemoClass

6 Assignment Create a class called DavosClass with the following methods. (Then create a client to test it.) Create a constructor. It receives the person’s age as a parameter, and it assigns this value to an instance variable in the class. ageDiv() – accepts no parameters, returns true if the person’s age is divisible by 5, false if not abs() - accepts one number as a parameter, returns the absolute value of that number nextToLast() - accepts one word as a parameter, returns the next-to-last letter in the word getName() – accepts no parameters; asks for the user’s name, then returns it, with an insult attached to it ***More on next slide…

7 displayStuff() – accepts a num and a word, displays that word num times, returns nothing findIndex() – accepts a word and a letter as parameters, returns which index that letter was found at in the word (ignore multiple occurrences of the letter), returns 999 if the letter is not found primeNum() - receives a number, returns true if it is prime, false if not. (make sure to test this!) getPrimez( ) -- Accepts two #s, returns how many primes exist between (and including) the two numbers. sumOfDigits( ) – accepts a 3-digit number, returns the sum of its digits


Download ppt "Constructors A constructor is a method that has the same name as the class itself It is automatically called when an object is instantiated (in other words,"

Similar presentations


Ads by Google