Register   Login   About   Study   Enterprise   Share
AI / Internet Technology University (AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 1. Java Introduction
Current Topic: 1.2.5. OOP in Java
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
Java is an Object-Oriented language.

What does it mean?
First of all the language supports the concept of objects as data structures. There are primitive data types, such as int (integer), double, char (character), and more. There are built-in the language classes, or more complex data types, such as Integer, String, ArrayList, and more. But the most compelling feature is ability to add to the language user-defined classes with their composite data structures and the methods to manage these new data types. There are millions of classes created by Java developers and available to us in multiple libraries. And we continue expanding the language by adding new classes each time we need to solve a business problem with a new Java-based application or a service.
When we create a new class composite of known data types or fields, we exercise one of the fundamental OOP concepts, Encapsulation.
Following this concept, we make the fields in a class private and provide access to the data via public methods. The only way to access and manage the fields in the class, is via these methods.
Here are some examples of user-defined Java classes: CocaColaSoda and PepsiSoda.
Open Eclipse and create the package day3 under the project week1. Then follow the example below and create the class CocaColaSoda under the package day3.
Note that we use the keyword this to allow a program to distinguish the class name sugar from the same name passed to the method as an argument (parameter). It is only necessary if the names are the same as in this sample

package day3;
/**
* CocaColaSoda has some amount of calories, sugar, and caffeine as internal data structure
* The data are private, and there are the methods, such as get and set to manage these data.
* There is also the method calculateHealthIndex will uses a formula to calculate the health index
* (This is just an example of programming, not real health guidance :-)
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public class CocaColaSoda {

// data ? keep data private
private int calories;
private int sugar;
private int caffeine;
// methods ? make methods public to allow managing data
public int getSugar() {
return sugar;
}

public void setSugar(int sugar) {
// the keyword this allows a program to distinguish the class name sugar from the same name passed to the method as an argument (parameter).
// It is only necessary if the names are the same as in this sample
this.sugar = sugar;
}

public int getCaffeine() {
return caffeine;
}

public void setCaffeine(int caffeine) {
this.caffeine = caffeine;
}
public int getCalories() {
return calories;
}

public void setCalories(int calories) {
this.calories = calories;
}
/**
* The method calculateHealthIndex multiplies sugar * caffeine * calories to calculate the healthIndex
* @return healthIndex
*/
public int calculateHealthIndex() {
int healthIndex = sugar * caffeine * calories;
return healthIndex;
}
/**
* The main method will test the methods above by providing input parameters and displaying the results
* In the future, JUnit utilities can be used to automatically provide such testing
* @author Jeff.Zhuk@JavaSchool.com
*/

public static void main(String[] args) {
// first of all let us create an object of the class CocaColaSoda
// CocaColaSoda is the class name, which must be spelled exactly as defined above
// coca is the object name, which can be anything we want, but recommended to be close to the class name and start with a small letter
// new CocaColaSoda(); - the way to call a constructor to create the coca object of the CocaColaSoda class
CocaColaSoda coca = new CocaColaSoda();
coca.setCalories(5); // call the method setCalories and pass the integer 5
coca.setCaffeine(3); // call the method setCaffeine and pass the integer 3
coca.setSugar(6); // call the method setSugar and pass the integer 6
int healthIndex = coca.calculateHealthIndex(); // call the method calculateHealthIndex, which will return the result of calculations
System.out.println("healthIndex = "+ healthIndex); // display the result
}

}

Before going to the next sample, please create and run the CocaColaSoda class in Eclipse.
But Coca Cola is not only soda available. While my daughter completely rejects any soda, I sometimes, although not very often, drink Pepsi. Let us create a similar class PepsiSoda

package day3oop;
/**
* PepsiSoda has some amount of sugar, caffeine, and sodium as internal data structure
* The data are private, and there are the methods, such as get and set to manage these data.
* There is also the method calculateHealthIndex will uses a formula to calculate the health index
* (This is just an example of programming, not real health guidance :-)
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public class PepsiSoda {

// data ? keep data private
private int sodium;
private int sugar;
private int caffeine;
// methods ? make methods public to allow managing data
public int getSugar() {
return sugar;
}

public void setSugar(int sugar) {
// the keyword this allows a program to distinguish the class name sugar from the same name passed to the method as an argument (parameter).
// It is only necessary if the names are the same as in this sample
this.sugar = sugar;
}

public int getCaffeine() {
return caffeine;
}

public void setCaffeine(int caffeine) {
this.caffeine = caffeine;
}
public int getSodium() {
return sodium;
}

public void setSodium(int sodium) {
this.sodium = sodium;
}
/**
* The method calculateHealthIndex multiplies sugar * caffeine * sodium to calculate the healthIndex
* @return healthIndex
*/
public int calculateHealthIndex() {
int healthIndex = sugar * caffeine * sodium;
return healthIndex;
}
/**
* The main method will test the methods above by providing input parameters and displaying the results
* In the future, JUnit utilities can be used to automatically provide such testing
* @author Jeff.Zhuk@JavaSchool.com
*/

public static void main(String[] args) {
// first of all let us create an object of the class PepsiSoda
// PepsiSoda is the class name, which must be spelled exactly as defined above
// pepsi is the object name, which can be anything we want, but recommended to be close to the class name and start with a small letter
// new PepsiSoda(); - the way to call a constructor to create the pepsi object of the PepsiSoda class
PepsiSoda pepsi = new PepsiSoda();
pepsi.setSodium(5); // call the method setSodium and pass the integer 5
pepsi.setCaffeine(3); // call the method setCaffeine and pass the integer 3
pepsi.setSugar(6); // call the method setSugar and pass the integer 6
int healthIndex = pepsi.calculateHealthIndex(); // call the method calculateHealthIndex, which will return the result of calculations
System.out.println("healthIndex = "+ healthIndex); // display the result
}
}

Before going to the next sample, please create and run the PepsiSoda class in Eclipse.
Did you notice some similarities in both classes, PepsiSoda and CocaColaSoda?
Yes, both are about some sort of Soda. Both classes have common fields, such as caffeine and sugar and related methods. And potentially we might have more Soda-based classes. This is a great place to introduce two more OOP concepts: Inheritance and Polymorphism. We start with Inheritance as Polymorphism cannot exist without Inheritance anyway.
The idea of Inheritance is to collect common fields of two or more classes in a parent class or superclass and then have in so called subclasses only their specific not common fields and methods. By introducing a new class, a superclass, we allow to reduce code in other classes, subclasses.
Let us implement this idea for our Soda-based classes. We will create the Soda class with the common fields, sugar and caffeine and related methods. Then, we will create derived classes, Pepsi and CocaCola, which inherit from or extends (this is a Java keyword!) the superclass Soda.
As a result of this exercise, you will add to the package day3 three more classes: Soda, Pepsi, and CocaCola.
Note, that we provide protected but not private data in the superclass Soda. Why? Because it is important that subclasses would have access to the data they inherited. Making data protected is the compromise that allow data to be visible to subclasses but not to other classes.

package day3;
/**
* Soda has: sugar, caffeine
* calculateHealthIndex() as sugar * caffeine
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public class Soda {
// data ? in a superclass must be protected, not private, to be visible by subclasses
protected int sugar;
protected int caffeine;
// methods
public int getSugar() {
return sugar;
}

public void setSugar(int sugar) {
this.sugar = sugar;
}

public int getCaffeine() {
return caffeine;
}

public void setCaffeine(int caffeine) {
this.caffeine = caffeine;
}
/**
* Calculate the healthIndex as sugar * caffeine
* @return healthIndex
*/
public int calculateHealthIndex() {
int healthIndex = sugar * caffeine;
return healthIndex;
}
/**
* The main method will test the methods above by providing input parameters and displaying the results
* In the future, JUnit utilities can be used to automatically provide such testing
* @author Jeff.Zhuk@JavaSchool.com
*/

public static void main(String[] args) {
// first of all let us create an object of the class Soda
// Soda is the class name, which must be spelled exactly as defined above
// soda is the object name, which can be anything we want, but recommended to be close to the class name and start with a small letter
// new Soda(); - the way to call a constructor to create the soda object of the Soda class
Soda soda = new Soda();
soda.setCaffeine(3); // call the method setCaffeine and pass the integer 3
soda.setSugar(6); // call the method setSugar and pass the integer 6
int healthIndex = soda.calculateHealthIndex(); // call the method calculateHealthIndex, which will return the result of calculations
System.out.println("healthIndex = "+ healthIndex); // display the result
}

}
Was it clear so far?

Before going to the next sample, please create and run the Soda class in Eclipse.


package day3;
/**
* Pepsi makes it even better with sodium
* healthIndex = sugar * caffeine * sodium
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public class Pepsi extends Soda {
// data
private int sodium; // the only other field besides the common fields defined in the superclass Soda
// methods
public int getSodium() {
return sodium;
}

public void setSodium(int sodium) {
this.sodium = sodium;
}
/**
* Calculate the healthIndex as sugar * caffeine * sodium
* @return healthIndex
*/
public int calculateHealthIndex() {
int healthIndex = sugar * caffeine * sodium;
return healthIndex;
}
/**
* The main method will test the methods above by providing input parameters and displaying the results
* In the future, JUnit utilities can be used to automatically provide such testing
* @author Jeff.Zhuk@JavaSchool.com
*/

public static void main(String[] args) {
// first of all let us create an object of the class Pepsi
// Pepsi is the class name, which must be spelled exactly as defined above
// pepsi is the object name, which can be anything we want, but recommended to be close to the class name and start with a small letter
// new Pepsi(); - the way to call a constructor to create the pepsi object of the Pepsi class
Pepsi pepsi = new Pepsi();
pepsi.setSodium(5); // call the method setSodium and pass the integer 5
pepsi.setCaffeine(3); // call the method setCaffeine and pass the integer 3
pepsi.setSugar(6); // call the method setSugar and pass the integer 6
int healthIndex = pepsi.calculateHealthIndex(); // call the method calculateHealthIndex, which will return the result of calculations
System.out.println("healthIndex = "+ healthIndex); // display the result
}
}

Before going to the next sample, please create and run the Pepsi class in Eclipse.

/**
* Coca has calories besides everything in Soda
* The method calculateHealthIndex will include * calories
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public class CocaCola extends Soda {
// data
private int calories; // the only other field besides the common fields defined in the superclass Soda
// methods

public int getCalories() {
return calories;
}

public void setCalories(int calories) {
this.calories = calories;
}
/**
* The method calculateHealthIndex multiplies sugar * caffeine * calories to calculate the healthIndex
* @return healthIndex
*/
public int calculateHealthIndex() {
int healthIndex = sugar * caffeine * calories;
return healthIndex;
}

/**
* The main method will test the methods above by providing input parameters and displaying the results
* In the future, JUnit utilities can be used to automatically provide such testing
* @author Jeff.Zhuk@JavaSchool.com
*/

public static void main(String[] args) {
// first of all let us create an object of the class CocaCola
// CocaCola is the class name, which must be spelled exactly as defined above
// coca is the object name, which can be anything we want, but recommended to be close to the class name and start with a small letter
// new CocaCola(); - the way to call a constructor to create the coca object of the CocaCola class
CocaCola coca = new CocaCola();
coca.setCalories(5); // call the method setCalories and pass the integer 5
coca.setCaffeine(3); // call the method setCaffeine and pass the integer 3
coca.setSugar(6); // call the method setSugar and pass the integer 6
int indexOfHealth = coca.calculateHealthIndex(); // call the method calculateHealthIndex, which will return the result of calculations
System.out.println("indexOfHealth="+indexOfHealth); // display the result
}
}

Before going to the next sample, please create and run the CocaCola class in Eclipse.
There is one method that we repeat in all three classes, although each class implemented this method differently. Which method? - calculateHealthIndex. Even this method is defined in the Soda class, we redefine this method in the derived classes. This ability to change object behavior or methods based on object type or class is another OOP concept, Polymorphism. Polymorphism is available for the objects of a family of objects. In our sample, there is the family of Soda objects. We can say that these objects take different forms: Pepsi or CocaCola. Then, depending on the form or a derived class, the method calculateHealthIndex works differently for each class.
Here is the TestSoda class that explores the ability of the derived classes, Pepsi and CocaCola to redefine their method calculateHealthIndex. Note, that the program treats Pepsi as well as CocaCola as Soda classes. But at run-time, specific objects of Pepsi or CocaCola are recognized by Java Virtual Machine (JVM) and a proper method calculateHealthIndex of a proper class is going to be invoked.

package day3;
/**
* Have Pepsi and Coca in array of Soda
* Set pepsi and cola with their specific data
* In the for loop get next Soda and calculate health index
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public class TestSoda {
public static void main(String[] args) {
// data
Pepsi pepsi = new Pepsi();
CocaCola coca = new CocaCola();
Soda[] sodas = {pepsi, coca};
pepsi.setSodium(35);
coca.setCalories(150);
// arrange for loop for soda objects
for(int i=0; i < sodas.length; i++) {
sodas[i].setSugar(41);
sodas[i].setCaffeine(21);
int healthIndex = sodas[i].calculateHealthIndex();
System.out.println("Soda is healthy as "+healthIndex);
}
}
}

Assignments:
1. Under the package day3 create a similar family of classes, such as Car, Ford, Toyota to demonstrate inheritance and provide the TestCar class to demonstrate Polymorphism
2. Answer the QnA and create at least 3 more QnA related to the subject.
3. Highly recommended reading:
http://ITUniversity.us/school/public/sto.html
http://ITUniversity.us/school/public/oop/oop.html
http://ITUniversity.us/school/public/javalab/jschool.htm
Topic Graph | Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/>package day3;
<br/>/**
<br/> * CocaColaSoda has some amount of calories, sugar, and caffeine as internal data structure
<br/> * The data are private, and there are the methods, such as get and set to manage these data.
<br/> * There is also the method calculateHealthIndex will uses a formula to calculate the health index
<br/> * (This is just an example of programming, not real health guidance :-)
<br/> * @author Jeff.Zhuk@JavaSchool.com
<br/> *
<br/> */
<br/>public class CocaColaSoda {
<br/>
<br/>	// data ? keep data private
<br/>	private int calories;
<br/>	private int sugar;
<br/>	private int caffeine;
<br/>	// methods ? make methods public to allow managing data
<br/>	public int getSugar() {
<br/>		return sugar;
<br/>	}
<br/>
<br/>	public void setSugar(int sugar) {
<br/>// the keyword <b>this</b> allows a program to distinguish the class name <b>sugar</b> from the same name passed to the method as an argument (parameter). 
<br/>// It is only necessary if the names are the same as in this sample
<br/>		this.sugar = sugar;
<br/>	}
<br/>
<br/>	public int getCaffeine() {
<br/>		return caffeine;
<br/>	}
<br/>
<br/>	public void setCaffeine(int caffeine) {
<br/>		this.caffeine = caffeine;
<br/>	}	
<br/>	public int getCalories() {
<br/>		return calories;
<br/>	}
<br/>
<br/>	public void setCalories(int calories) {
<br/>		this.calories = calories;
<br/>	}
<br/>	/**
<br/>	 * The method calculateHealthIndex multiplies sugar * caffeine * calories to calculate the healthIndex
<br/>	 * @return healthIndex
<br/>	 */
<br/>	public int calculateHealthIndex() {
<br/>		int healthIndex = sugar * caffeine * calories;
<br/>		return healthIndex;
<br/>	}	
<br/>	/**
<br/>	 * The main method will test the methods above by providing input parameters and displaying the results
<br/>	 * In the future, JUnit utilities can be used to automatically provide such testing
<br/>	 * @author Jeff.Zhuk@JavaSchool.com
<br/>	 */
<br/>
<br/>	public static void main(String[] args) {
<br/>		// first of all let us create an object of the class CocaColaSoda
<br/>		// CocaColaSoda is the class name, which must be spelled exactly as defined above
<br/>		// coca is the object name, which can be anything we want, but recommended to be close to the class name and start with a small letter
<br/>		// new CocaColaSoda(); - the way to call a <b>constructor</b> to create the <b>coca</b> object of the CocaColaSoda class
<br/>		CocaColaSoda coca = new CocaColaSoda(); 
<br/>		coca.setCalories(5); // call the method setCalories and pass the integer 5
<br/>		coca.setCaffeine(3); // call the method setCaffeine and pass the integer 3
<br/>		coca.setSugar(6);  // call the method setSugar and pass the integer 6
<br/>		int healthIndex = coca.calculateHealthIndex(); // call the method calculateHealthIndex, which will return the result of calculations
<br/>		System.out.println("healthIndex = "+ healthIndex); // display the result
<br/>	}
<br/>
<br/>}
<br/>

Before going to the next sample, please create and run the CocaColaSoda class in Eclipse.
But Coca Cola is not only soda available. While my daughter completely rejects any soda, I sometimes, although not very often, drink Pepsi. Let us create a similar class PepsiSoda
<br/>package day3oop;
<br/>/**
<br/> * PepsiSoda has some amount of sugar, caffeine, and sodium as internal data structure
<br/> * The data are private, and there are the methods, such as get and set to manage these data.
<br/> * There is also the method calculateHealthIndex will uses a formula to calculate the health index
<br/> * (This is just an example of programming, not real health guidance :-)
<br/> * @author Jeff.Zhuk@JavaSchool.com
<br/> *
<br/> */
<br/>public class PepsiSoda {
<br/>
<br/>	// data ? keep data private
<br/>	private int sodium;
<br/>	private int sugar;
<br/>	private int caffeine;
<br/>	// methods ? make methods public to allow managing data
<br/>	public int getSugar() {
<br/>		return sugar;
<br/>	}
<br/>
<br/>	public void setSugar(int sugar) {
<br/>// the keyword <b>this</b> allows a program to distinguish the class name <b>sugar</b> from the same name passed to the method as an argument (parameter). 
<br/>// It is only necessary if the names are the same as in this sample
<br/>		this.sugar = sugar;
<br/>	}
<br/>
<br/>	public int getCaffeine() {
<br/>		return caffeine;
<br/>	}
<br/>
<br/>	public void setCaffeine(int caffeine) {
<br/>		this.caffeine = caffeine;
<br/>	}	
<br/>	public int getSodium() {
<br/>		return sodium;
<br/>	}
<br/>
<br/>	public void setSodium(int sodium) {
<br/>		this.sodium = sodium;
<br/>	}
<br/>	/**
<br/>	 * The method calculateHealthIndex multiplies sugar * caffeine * sodium to calculate the healthIndex
<br/>	 * @return healthIndex
<br/>	 */
<br/>	public int calculateHealthIndex() {
<br/>		int healthIndex = sugar * caffeine * sodium;
<br/>		return healthIndex;
<br/>	}	
<br/>	/**
<br/>	 * The main method will test the methods above by providing input parameters and displaying the results
<br/>	 * In the future, JUnit utilities can be used to automatically provide such testing
<br/>	 * @author Jeff.Zhuk@JavaSchool.com
<br/>	 */
<br/>
<br/>	public static void main(String[] args) {
<br/>		// first of all let us create an object of the class PepsiSoda
<br/>		// PepsiSoda is the class name, which must be spelled exactly as defined above
<br/>		// pepsi is the object name, which can be anything we want, but recommended to be close to the class name and start with a small letter
<br/>		// new PepsiSoda(); - the way to call a <b>constructor</b> to create the <b>pepsi</b> object of the PepsiSoda class
<br/>		PepsiSoda pepsi = new PepsiSoda(); 
<br/>		pepsi.setSodium(5); // call the method setSodium and pass the integer 5
<br/>		pepsi.setCaffeine(3); // call the method setCaffeine and pass the integer 3
<br/>		pepsi.setSugar(6);  // call the method setSugar and pass the integer 6
<br/>		int healthIndex = pepsi.calculateHealthIndex(); // call the method calculateHealthIndex, which will return the result of calculations
<br/>		System.out.println("healthIndex = "+ healthIndex); // display the result
<br/>	}
<br/>}
<br/>

Before going to the next sample, please create and run the PepsiSoda class in Eclipse.
Did you notice some similarities in both classes, PepsiSoda and CocaColaSoda?
Yes, both are about some sort of Soda. Both classes have common fields, such as caffeine and sugar and related methods. And potentially we might have more Soda-based classes. This is a great place to introduce two more OOP concepts: Inheritance and Polymorphism. We start with Inheritance as Polymorphism cannot exist without Inheritance anyway.
The idea of Inheritance is to collect common fields of two or more classes in a parent class or superclass and then have in so called subclasses only their specific not common fields and methods. By introducing a new class, a superclass, we allow to reduce code in other classes, subclasses.
Let us implement this idea for our Soda-based classes. We will create the Soda class with the common fields, sugar and caffeine and related methods. Then, we will create derived classes, Pepsi and CocaCola, which inherit from or extends (this is a Java keyword!) the superclass Soda.
As a result of this exercise, you will add to the package day3 three more classes: Soda, Pepsi, and CocaCola.
Note, that we provide protected but not private data in the superclass Soda. Why? Because it is important that subclasses would have access to the data they inherited. Making data protected is the compromise that allow data to be visible to subclasses but not to other classes.
<br/>package day3;
<br/>/**
<br/> * Soda has: sugar, caffeine
<br/> * calculateHealthIndex() as sugar * caffeine
<br/> * @author Jeff.Zhuk@JavaSchool.com
<br/> *
<br/> */
<br/>public class Soda {
<br/>	// data ? in a superclass must be protected, not private, to be visible by subclasses
<br/>	protected int sugar;
<br/>	protected int caffeine;
<br/>	// methods
<br/>	public int getSugar() {
<br/>		return sugar;
<br/>	}
<br/>
<br/>	public void setSugar(int sugar) {
<br/>		this.sugar = sugar;
<br/>	}
<br/>
<br/>	public int getCaffeine() {
<br/>		return caffeine;
<br/>	}
<br/>
<br/>	public void setCaffeine(int caffeine) {
<br/>		this.caffeine = caffeine;
<br/>	}
<br/>	/**
<br/>	 * Calculate the healthIndex as sugar * caffeine
<br/>	 * @return healthIndex
<br/>	 */
<br/>	public int calculateHealthIndex() {
<br/>		int healthIndex = sugar * caffeine;
<br/>		return healthIndex;
<br/>	}
<br/>	/**
<br/>	 * The main method will test the methods above by providing input parameters and displaying the results
<br/>	 * In the future, JUnit utilities can be used to automatically provide such testing
<br/>	 * @author Jeff.Zhuk@JavaSchool.com
<br/>	 */
<br/>
<br/>	public static void main(String[] args) {
<br/>		// first of all let us create an object of the class Soda
<br/>		// Soda is the class name, which must be spelled exactly as defined above
<br/>		// soda is the object name, which can be anything we want, but recommended to be close to the class name and start with a small letter
<br/>		// new Soda(); - the way to call a <b>constructor</b> to create the <b>soda</b> object of the Soda class
<br/>		Soda soda = new Soda(); 
<br/>		soda.setCaffeine(3); // call the method setCaffeine and pass the integer 3
<br/>		soda.setSugar(6);  // call the method setSugar and pass the integer 6
<br/>		int healthIndex = soda.calculateHealthIndex(); // call the method calculateHealthIndex, which will return the result of calculations
<br/>		System.out.println("healthIndex = "+ healthIndex); // display the result
<br/>	}
<br/>
<br/>}
<br/>






Was it clear so far?


Before going to the next sample, please create and run the Soda class in Eclipse.

<br/>package day3;
<br/>/**
<br/> * Pepsi makes it even better with sodium
<br/> * healthIndex = sugar * caffeine * sodium
<br/> * @author Jeff.Zhuk@JavaSchool.com
<br/> *
<br/> */
<br/>public class Pepsi extends Soda {
<br/>	// data
<br/>	private int sodium; // the only other field besides the common fields defined in the superclass Soda
<br/>	// methods
<br/>	public int getSodium() {
<br/>		return sodium;
<br/>	}
<br/>
<br/>	public void setSodium(int sodium) {
<br/>		this.sodium = sodium;
<br/>	}
<br/>	/**
<br/>	 * Calculate the healthIndex as sugar * caffeine * sodium
<br/>	 * @return healthIndex
<br/>	 */
<br/>	public int calculateHealthIndex() {
<br/>		int healthIndex = sugar * caffeine * sodium;
<br/>		return healthIndex;
<br/>	}
<br/>	/**
<br/>	 * The main method will test the methods above by providing input parameters and displaying the results
<br/>	 * In the future, JUnit utilities can be used to automatically provide such testing
<br/>	 * @author Jeff.Zhuk@JavaSchool.com
<br/>	 */
<br/>
<br/>	public static void main(String[] args) {
<br/>		// first of all let us create an object of the class Pepsi
<br/>		// Pepsi is the class name, which must be spelled exactly as defined above
<br/>		// pepsi is the object name, which can be anything we want, but recommended to be close to the class name and start with a small letter
<br/>		// new Pepsi(); - the way to call a <b>constructor</b> to create the <b>pepsi</b> object of the Pepsi class
<br/>		Pepsi pepsi = new Pepsi(); 
<br/>		pepsi.setSodium(5); // call the method setSodium and pass the integer 5
<br/>		pepsi.setCaffeine(3); // call the method setCaffeine and pass the integer 3
<br/>		pepsi.setSugar(6);  // call the method setSugar and pass the integer 6
<br/>		int healthIndex = pepsi.calculateHealthIndex(); // call the method calculateHealthIndex, which will return the result of calculations
<br/>		System.out.println("healthIndex = "+ healthIndex); // display the result
<br/>	}
<br/>}
<br/>

Before going to the next sample, please create and run the Pepsi class in Eclipse.
<br/>/**
<br/> * Coca has calories besides everything in Soda
<br/> * The method calculateHealthIndex will include * calories
<br/> * @author Jeff.Zhuk@JavaSchool.com
<br/> *
<br/> */
<br/>public class CocaCola extends Soda {
<br/>	// data
<br/>	private int calories; // the only other field besides the common fields defined in the superclass Soda
<br/>	// methods
<br/>	
<br/>	public int getCalories() {
<br/>		return calories;
<br/>	}
<br/>
<br/>	public void setCalories(int calories) {
<br/>		this.calories = calories;
<br/>	}
<br/>	/**
<br/>	 * The method calculateHealthIndex multiplies sugar * caffeine * calories to calculate the healthIndex
<br/>	 * @return healthIndex
<br/>	 */
<br/>	public int calculateHealthIndex() {
<br/>		int healthIndex = sugar * caffeine * calories;
<br/>		return healthIndex;
<br/>	}
<br/>
<br/>	/**
<br/>	 * The main method will test the methods above by providing input parameters and displaying the results
<br/>	 * In the future, JUnit utilities can be used to automatically provide such testing
<br/>	 * @author Jeff.Zhuk@JavaSchool.com
<br/>	 */
<br/>
<br/>	public static void main(String[] args) {
<br/>		// first of all let us create an object of the class CocaCola
<br/>		// CocaCola is the class name, which must be spelled exactly as defined above
<br/>		// coca is the object name, which can be anything we want, but recommended to be close to the class name and start with a small letter
<br/>		// new CocaCola(); - the way to call a <b>constructor</b> to create the <b>coca</b> object of the CocaCola class
<br/>		CocaCola coca = new CocaCola(); 
<br/>		coca.setCalories(5); // call the method setCalories and pass the integer 5
<br/>		coca.setCaffeine(3); // call the method setCaffeine and pass the integer 3
<br/>		coca.setSugar(6);  // call the method setSugar and pass the integer 6
<br/>		int indexOfHealth = coca.calculateHealthIndex(); // call the method calculateHealthIndex, which will return the result of calculations
<br/>		System.out.println("indexOfHealth="+indexOfHealth); // display the result
<br/>	}
<br/>}
<br/>

Before going to the next sample, please create and run the CocaCola class in Eclipse.
There is one method that we repeat in all three classes, although each class implemented this method differently. Which method? - calculateHealthIndex. Even this method is defined in the Soda class, we redefine this method in the derived classes. This ability to change object behavior or methods based on object type or class is another OOP concept, Polymorphism. Polymorphism is available for the objects of a family of objects. In our sample, there is the family of Soda objects. We can say that these objects take different forms: Pepsi or CocaCola. Then, depending on the form or a derived class, the method calculateHealthIndex works differently for each class.
Here is the TestSoda class that explores the ability of the derived classes, Pepsi and CocaCola to redefine their method calculateHealthIndex. Note, that the program treats Pepsi as well as CocaCola as Soda classes. But at run-time, specific objects of Pepsi or CocaCola are recognized by Java Virtual Machine (JVM) and a proper method calculateHealthIndex of a proper class is going to be invoked.
<br/>package day3;
<br/>/**
<br/> * Have Pepsi and Coca in array of Soda
<br/> * Set pepsi and cola with their specific data
<br/> * In the for loop get next Soda and calculate health index
<br/> * @author Jeff.Zhuk@JavaSchool.com
<br/> *
<br/> */
<br/>public class TestSoda {
<br/>	public static void main(String[] args) {
<br/>		// data
<br/>		Pepsi pepsi = new Pepsi();
<br/>		CocaCola coca = new CocaCola();
<br/>		Soda[] sodas = {pepsi, coca};
<br/>		pepsi.setSodium(35);
<br/>		coca.setCalories(150);
<br/>		// arrange for loop for soda objects
<br/>		for(int i=0; i < sodas.length; i++) {
<br/>			sodas[i].setSugar(41);
<br/>			sodas[i].setCaffeine(21);
<br/>			int healthIndex = sodas[i].calculateHealthIndex();
<br/>			System.out.println("Soda is healthy as "+healthIndex);
<br/>		}
<br/>	}
<br/>}
<br/>

Assignments:
1. Under the package day3 create a similar family of classes, such as Car, Ford, Toyota to demonstrate inheritance and provide the TestCar class to demonstrate Polymorphism
2. Answer the QnA and create at least 3 more QnA related to the subject.
3. Highly recommended reading:
http://ITUniversity.us/school/public/sto.html
http://ITUniversity.us/school/public/oop/oop.html
http://ITUniversity.us/school/public/javalab/jschool.htm
Topic Graph | Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy