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

(3) Briefly (in just a few words), describe the difference between an instance and a class.
(3) Briefly (in just a few words), describe the difference between an instance and a class.
Java homework question: Explain how class (static) variables and methods differ from their instance counterparts. Give...
Java homework question: Explain how class (static) variables and methods differ from their instance counterparts. Give an example of a class that contains at least one class variable and at least one class method. Explain why using a class variable and method rather than an instance variable and method would be the correct choice in the example you select.
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.
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT