Question

In: Computer Science

1. Describe the difference between instance methods and class methods in Java and give an example...

1. Describe the difference between instance methods and class methods in Java and give an example of each.

2. A class variable is visible to and shared by all instances of a class. How would such a variable be used in an application?

3. Describe the difference between abstract classes and concrete classes, giving an example of each.

4. Explain how data are encapsulated and information is hidden in Java?

5. Explain the difference between a class and an interface in Java, using at least one example.

Solutions

Expert Solution

Instance methods can access class variables and class methods directly. Class methods can access class variables and class methods directly. Class methods cannot access instance variables or instance methods directly they must use an object reference.

They both are member variables, meaning that both are associated with a class. Now of course, there are differences between the two:

Instance variables:

These variables belong to the instance of a class, thus an object. And every instance of that class (object) has it's own copy of that variable. Changes made to the variable don't reflect in other instances of that class.

Class variables:

These are also known as static member variables and there's only one copy of that variable that is shared with all instances of that class. If changes are made to that variable, all other instances will see the effect of the changes.

The only real difference is that a concrete class can be instantiated because it provides (or inherits) the implementation for all of its methods. An abstract class cannot be instantiated because at least one method has not been implemented.

The primary use for encapsulation is to hide implementation details from calling code. When calling code wants to retrieve a value, it should not depend on from wherethe value comes. Internally, the class can store the value in a field or retrieve it from some external resource


Related Solutions

Give an example of an inner and outer class. (Java)
Give an example of an inner and outer class. (Java)
true or false give reason language in java 1.A static inner class can access the instance...
true or false give reason language in java 1.A static inner class can access the instance variables and methods of its outer non-static class 2.executeQuery from statement may return more than one resultset object 3.Connection is java.sql interface that establishes a session with a specific database 4.Writable is an interface in Hadoop that acts as a wrapper class to almost all the primitive data type 5.Text is the wrapper class of string in Hadoop
Describe the difference between a repressible operon and an inducible operon. Give an example of either...
Describe the difference between a repressible operon and an inducible operon. Give an example of either type of operon and describe the mechanism by which the operon responds to an environmental cue. Describe the differences between general transcription factors and specific transcription factors and their impact on gene expression. Compare and contrast microRNA (miRNA) and synthetic RNA (siRNA). Be sure to include their occurrence in nature, and their mechanism(s) of action.
Describe the difference between exposure and contamination and why this distinction is important. Give an example...
Describe the difference between exposure and contamination and why this distinction is important. Give an example to illustrate your description.
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give...
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give in the end of question) a method to return the botanical (Latin) name of the plant a method that describes how the plant is used by humans (as food, to build houses, etc) Add a Vegetable class with a flavor variable (sweet, salty, tart, etc) and 2 methods that return the following information: list 2 dishes (meals) that the vegetable can be used in...
Describe the difference between a service and merchandising firm. Give an example of each from publicly...
Describe the difference between a service and merchandising firm. Give an example of each from publicly traded companies. Give a detailed example of the recording of purchases under a perpetual inventory system including returns, allowances and discounts. Give a detailed example of the recording sales revenues under a perpetual inventory system including returns, allowances and discounts. Give a detailed example of the recording of purchases under a periodic inventory system including returns, allowances and discounts. Give a detailed example of...
Define partial and semi partial correlation and describe the difference between them. Give an example of...
Define partial and semi partial correlation and describe the difference between them. Give an example of each.
Describe the difference between a service and merchandising firm. Give an example of each from publicly...
Describe the difference between a service and merchandising firm. Give an example of each from publicly traded companies. Give a detailed example of the recording of purchases under a perpetual inventory system including returns, allowances and discounts. Give a detailed example of the recording sales revenues under a perpetual inventory system including returns, allowances and discounts. Give a detailed example of the recording of purchases under a periodic inventory system including returns, allowances and discounts. Give a detailed example of...
1-a)What is the difference between an experiment and an observational study? b)Give an example of an...
1-a)What is the difference between an experiment and an observational study? b)Give an example of an experiment. In your example, what population are you studying? 2-a)Define the mode and median of a set of data points. b)Construct a set of 5 data points with a mode of 3. Construct a second set of 5 data points with a median of 5. Clearly indicate which set is which. 3)Construct a frequency distribution using 4 classes for the following set of 10...
What is exception propagation? Give an example of a class that contains at least two methods,...
What is exception propagation? Give an example of a class that contains at least two methods, in which one method calls another. Ensure that the subordinate method will call a predefined Java method that can throw a checked exception. The subordinate method should not catch the exception. Explain how exception propagation will occur in your example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT