Question

In: Computer Science

(a) What is a class? What is an object? What is the relationship? (b) What are the instance variables and methods of a class?


 (a) What is a class? What is an object? What is the relationship?

 (b) What are the instance variables and methods of a class?

 (c) What is the effect of declaring instance variables and methods public or private?

 (d) Why do we often declare the instance variables of classes private?

 (e) Could we declare methods private? Would we want to do so? 

 (f) What does the identifier this mean? Give an example of its use (g) What is a constructor?

 (h) What is inheritance? Why is it useful?


Solutions

Expert Solution

a.
class:class is a model for creating objects and doesn't exist physically.a group of related things which will doesn't exist comes under a class

object:An object is anything that really exists in the world with certain properties

relationship:both the classes and objects contain variables and methods.

b.instance variables:instance variables are also kind of variables but which are declared inside a class not inside any method.which means they are useful to any method which are in same class.
       variables values can be different in different methods,they just available to all the methods inside a same class
method:In java method is useful to achieve a particular task.The statements inside a method are intended to achieve a particular sort of thing.

c.public:when we declared the instance variables or methods are public then they will available to other classes also.When we want to use the variables and methods of a class in other classes then it should be declared with public keyword.
private:we use private keyword to prevent usage for other classes.If they declared with private keyword then they only available for same class,not accessible or usable in any other classes.

d.It is just for security concerns.with the help of private keyword we can disable the use of access in other classes

e.Yes,we could declare methods as private.We can do this when this method has no use in other classes.There is a situation where a same method need to use in different classes.If there is no use with this method in other classes.
    then we can declare it as a private.All this can achieve with the help of concept called "inheritance"

f.this:"this" is a special keyword in java,which is used to invoke current class object.this refers to the object of the present class.At most this is used to achieve the clashes of variables or methods which have same names.
   It can point out the locality.

Example:

output:

g.constructor:constructor is used to initialize the instance variables.When we directly initialized the instance variables of a class then no need of writing any constructor, a default constructor can takes place all the work.
   with parameter passing we need to write a parametrized constructor.the name of the class name of constructor name should be same.

h.Inheritance:Acquiring the properties from it's previous classes is known as inheritance.Which means newly derived class can have the all the properties of derived class.The keyword 'extends' will be used.
   It is used when we need to work with it's previous class in present class ,without writing the entire code of previous class we just use inheritance concept.


Related Solutions

Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
Complete the required methods: public class SongList { // instance variables private Song m_last; private int...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int m_numElements; // constructor // Do not make any changes to this method! public SongList() { m_last = null; m_numElements = 0; } // check whether the list is empty // Do not make any changes to this method! boolean isEmpty() { if (m_last == null) return true; else return false; } // return the size of the list (# of Song nodes) // Do...
The Hole Class Description: For the Hole class, there are three instance variables: the par for...
The Hole Class Description: For the Hole class, there are three instance variables: the par for the hole, the length of the hole in yards, and a Boolean valued indicator of whether the hole is one in which the players’ full drives will be measured for distance. John clarifies: The length of the hole in yards determines par for the hole. The current standard is: No more than 250 yards Par 3 251 to 470 yards Par 4 471 to...
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.
Problem 3 Prototype object doubleQ A doubleQ object has 2 instance variables: firstQ and secondQ arrays...
Problem 3 Prototype object doubleQ A doubleQ object has 2 instance variables: firstQ and secondQ arrays that together implement a queue enqueue() instance method adds its argument to the end of this.secondQ dequeue() instance method removes and returns the element at the front of this.firstQ transfer() instance method removes the element from the front of this.secondQ, adds it to the end of this.firstQ If this.secondQ is empty, does nothing Test code is included in the file Download this file (with...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
Define a class for the student record. The class should instance variables for Quizzes, Midterm, Final...
Define a class for the student record. The class should instance variables for Quizzes, Midterm, Final and total score for the course and final letter grade. The class should have input and output methods. The input method should not ask for the final numeric grade, nor should it ask for final letter grade. The classes should have methods to compute the overall numeric grade and the final letter grade. These two methods will be void methods that set the appropriate...
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...
#Below is a class representing a person. You'll see the #Person class has three instance variables:...
#Below is a class representing a person. You'll see the #Person class has three instance variables: name, age, #and GTID. The constructor currently sets these values #via a calls to the setters. # #Create a new function called same_person. same_person #should take two instances of Person as arguments, and #returns True if they are the same Person, False otherwise. #Two instances of Person are considered to be the same if #and only if they have the same GTID. It does...
You have written a JAVA program that creates Shoe object with three instance variables. You will...
You have written a JAVA program that creates Shoe object with three instance variables. You will need to use the exception you created to go along with it. So far you have been using my driver classes. Now it is time for you to create your own driver class that will implement an ArrayList of Shoe objects. This assignment uses the Shoe.java and ShoeException.java to create the driver ShoeStore.Java You will display a menu as follows: Add shoes Print all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT