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?
(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?
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.