/* related posts with thumb nails */

Classes and Objects:

1. What is a class?

Class is a collection of members where members can be member variables or member functions.

2. What is an object?

Instances of classes are said to be objects. Members of a class are accessible by objects depending upon their accessibility conditions. Every object has to be instantiated before it is used.

3. What are methods?

Methods are member functions of a class. We can access methods of a class using object name and dot (if they are accessible). Static methods can be accessible with class name itself.

4. What are data members? Or What are properties?

The member variables of a class are called properties or attributes. These are also called “data members” or “fields”.

5. What are methods?

The member functions of a class can be called as “methods”. These are also called operations.

6. How do you create objects?

To create the instances of a class, we use the keyword new. The following shows the syntax to create the objects: class-name object-name = new class-name();

for example: Student s1=new Student() creates an object by name ‘s1’ that belongs to the class ‘student’

7. What is the structure of a class?

Structure of a class:

import package-name;

class class_name [extends super_class] [implements interface_name]



Member functions;


8. What is a Constructor?

Constructor is a member function that takes the same name as that of the class and it is called automatically when the objects are created.

9. What are the properties of a constructor?

Constructor can take arguments.

Constructors can be overloaded i.e. we can have any number of constructers provided they take different number or types of arguments.

Constructors do not specify any return type, not even void. This is because they return the instance of the class itself.

Generally, constructors are used for initializing the data members or for memory allocations, but it allows any type of statements.

10. What is constructor overloading?

Constructor overloading is having any number of constructors provided they take different number or types of arguments.

11. What are the types of constructors?

Constructor can be mainly of three types:

1. Default Constructor

2. Parameterized (overloaded) Constructor

3. Copy Constructor

12. What is a default constructor?

Default Constructor is a constructor that takes no arguments.

13. What is a Parameterized (overloaded) Constructor?

It is a constructor that takes arguments.

14. What is a copy constructor?

Copy Constructor is a constructor that takes the object of same class as argument.

15. What are Static data member (class variables):

Static data members (Class variables) are global to a class and belong to the entire set of objects that class creates. Only one memory location is created for each class variable. These are basically static data members of a class. They take common value for all the objects.

16. What are Static Methods?

· Static methods are the methods which behave same for all the objects of a class. Objects are not needed to call static methods.

· We can invoke static methods using class name itself i.e. without taking any instances of that class.

· Static methods can only call other static methods and refer to only static data members but they cannot access instance variables.

· They cannot refer to this or super in anyway.

17. What is call by reference?

Sending addresses as arguments while calling a function is call by reference. When we call a method by reference, the called-function sees the caller’s original variables passed as parameters, not copies. References to the called-function’s objects are treated the same way. Thus, any changes in the called-function affect the caller’s original variables. Java never uses call by reference. Java always uses call by value. It passes the objects by copy of reference.

Related Topics:


Post a Comment