question archive Describe how you would develop object-oriented features of Java for the Quiz program developed in the Programming Assignments

Describe how you would develop object-oriented features of Java for the Quiz program developed in the Programming Assignments

Subject:BusinessPrice:3.87 Bought7

Describe how you would develop object-oriented features of Java for the Quiz program developed in the Programming Assignments. In particular, describe how the program could use each of the following: 

class variables, instance variables, inheritance, polymorphism, abstract classes, "this", "super", interfaces, and event listeners

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:

class variables : Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. Static variables can be accessed by calling with the class name ClassName.VariableName.

 

instance variables : Instance variables are declared in a class, but outside a method, constructor or any block.

Instance variables can be accessed directly by calling the variable name inside the class. However, 

within static methods (when instance variables are given accessibility), they should be called using the fully qualified name. ObjectReference.VariableName.

 

Example:

 

public class VariableExample{
//instance variables
  int myInstanceVariable = 0;
//class variables
  static int staticData = 70;
  
  public static void main(String args[]){
   VariableExample obj = new VariableExample();
    
   System.out.println("Value of instance variable: "+obj.myInstanceVariable;);
   System.out.println("Value of static variable: "+VariableExample.staticData);
  }
}

Output:

 

Value of instance variable: 0
Value of static variable: 50

 

inheritance : An important feature of object-oriented programs is inheritance—the ability to create classes that share the attributes and methods of existing classes, but with more specific features. Inheritance is mainly used for code reusability.

 

Example

 

public abstract class Employee {


public abstract double getTotalSalary();


}
public class ProductionWorker extends Employee {

 public double getTotalSalary() {}

}

public class ShiftSupervisor  extends Employee {

 public double getTotalSalary() {}

}

public class TeamLeader extends ProductionWorker {

 public double getTotalSalary() {}

}

polymorphism : Java Polymorphism is one of easy concept to understand. Polymorphism definition is that Poly means many and morphos means forms. It describes the feature of languages that allows the same word or symbol to be interpreted correctly in different situations based on the context. There are two types of Polymorphism available in Java. For example, in English, the verb "run" means different things if you use it with "a footrace," a "business," or "a computer." You understand the meaning of "run" based on the other words used with it.

Example :

 

public abstract class Employee {

public abstract double getTotalSalary();

}



public class ProductionWorker extends Employee {

 public double getTotalSalary() {}

}

public class ShiftSupervisor extends Employee {

 public double getTotalSalary() {}

}

public class TeamLeader extends ProductionWorker {

 public double getTotalSalary() {}

}



 public static void main(String[] args) {
    // TODO code application logic here
    ArrayList<Employee> arraylistObject = new ArrayList<>();

    //Adding ShiftSupervisor to employee arraylist
    arraylistObject.add(new ShiftSupervisor());

    //Adding ProductionWorker to employee arraylist
    arraylistObject.add(new ProductionWorker());

    //Adding TeamLeader to employee arraylist
    arraylistObject.add(new TeamLeader());

    for (int i = 0; i < arraylistObject.size(); i++) {
      System.out.println(arraylistObject.get(i));

      System.out.println("Salary : $" + arraylistObject.get(i).getTotalSalary());

    }


 

 

 

abstract classes : A class that is declared using "abstract" keyword is known as abstract class.

 

 

Example

 

public abstract class Employee {

  // abstract method must be overridden by concrete subclasses
  public abstract double earnings(); // no implementation here
}

 

 

 

 

"this" : In many object-oriented programming languages, this is a variable that is used in instance methods to refer to the object on which they are working.

Also, this can be used to refer current class instance variable

 

Example

 

class Student{  
int rollno;  
String name;  
float fee;  
Student(int rollno,String name,float fee){  
//Using this to refer current class instance variable
this.rollno=rollno;  
this.name=name;  
this.fee=fee;  
}  

 

 

 

"super" : The super keyword refers to superclass objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.

 

 

Example

 

class Animal{  
String color="white";  
}  
class Cat extends Animal{  
String color="black";  
void printColor(){  
System.out.println(color);//prints color of Cat class  
System.out.println(super.color);//prints color of Animal class  
}  
}  
class TestSuper1{  
public static void main(String args[]){  
Cat d=new Cat();  
d.printColor();  
}}  

 

 

Interfaces : An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object.

Interfaces: are used to group related methods with empty bodies.

 

Example

 

// interface Animal 
interface Animal {
 public void makeAnimalSound(); // interface method (does not have a body)
 public void walk(); // interface method (does not have a body)
}

 

 

 

 

Event listeners : The Event listener represent the interfaces responsible to handle events. 

Java provides us various Event listener classes but we will discuss those which are more frequently used. 

Every method of an event listener method has a single argument as an object which is subclass of EventObject class

 

Example 

 

public class Customer ... implements ActionListener {
  ...
  //where initialization occurs:
    button.addActionListener(this);
  ...
  public void actionPerformed(ActionEvent e) {
    ...//Run Code When an action is performed...
  }
}