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...
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...
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...
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...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
You need to make an AngryBear class.(In java) The AngryBear class must have 2 instance variables....
You need to make an AngryBear class.(In java) The AngryBear class must have 2 instance variables. The first instance variable will store the days the bear has been awake. The second instance variable will store the number of teeth for the bear. The AngryBear class will have 1 constructor that takes in values for days awake and number of teeth. The AngryBear class will have one method isAngry(); An AngryBear is angry if it has been awake for more than...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode and numberOfEntries. It has an inner class Node. BagInterface.java /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.1*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /** Sees whether this bag is empty....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT