90 likes | 274 Views
About Homework for BPJ lesson 15 Overloading constructor Overloading methods Calling constructor/method Encapsulation Homework. Agenda. Overloading constructor. Constructors can be overloaded to provide more options for instantiating an object. Dog() - default constructor
E N D
About Homework for BPJ lesson 15 • Overloading constructor • Overloading methods • Calling constructor/method • Encapsulation • Homework Agenda
Overloading constructor • Constructors can be overloaded to provide more options for instantiating an object. Dog() - default constructor { breed = “Huskie”; color = “black”; leg = 4; size= ‘S’; }
Overloading constructor public Dog(String oBreed, String oColor) { breed = b; color = c; size = ‘L’; leg=4; } public Dog(String oBreed, String oColor, char oSize) { breed = oBreed; color = oColor; size = oSize; leg=4; }
How do we know which constructor is being called??? Dog aDog = new Dog(); Dog bDog = new Dog(“Terrier”, “brown”); Dog cDog = new Dog(“chihuahua”, “white”, ‘s’);
Overloading methods • Same method name with different number of parameters. public void eating(){ System.out.println(“eating dog food.”); } public void eating(String aFood){ System.out.println(“eating ” + aFood); }
BankAccount class • Create a BankAccount class. • This class has the following instance variables: • Balance, account number, password • A constructor which have 3 parameters. • This class has the following methods: getBalance() deposit() withdraw()
Encapsulation • Protecting an object’s data from code outside the class. • Those instance variables marked as private. • They can only be seen or modified through the use of public accessor and mutator methods. • Try type in the Cube class(next slide) in Eclipse and create a main(), instantiate a Cube object and type in <object>. to see the display list
public class Cube{ private int length; public int breadth; private int height; public Cube() { length = 10; breadth = 10; height = 10; } public Cube(int l, int b, int h) { length = l; breadth = b; height = h; } public int getVolume() { return (length * breadth * height); } }
Homework • BPJ Lesson 16 Project ..Gas mileage • Follow all the instructions to create an Automobile class and tester program