0% found this document useful (0 votes)
12 views

day 1 (1)

The document provides an overview of fundamental concepts in Java, including classes, objects, constructors, and memory allocation by the JVM. It explains the syntax for declaring classes, the purpose of constructors, and rules for defining them, along with access specifiers and constructor overloading. Additionally, it addresses common questions regarding constructors in interfaces and abstract classes, as well as the behavior of default and parameterized constructors.

Uploaded by

bale061699
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

day 1 (1)

The document provides an overview of fundamental concepts in Java, including classes, objects, constructors, and memory allocation by the JVM. It explains the syntax for declaring classes, the purpose of constructors, and rules for defining them, along with access specifiers and constructor overloading. Additionally, it addresses common questions regarding constructors in interfaces and abstract classes, as well as the behavior of default and parameterized constructors.

Uploaded by

bale061699
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Day 1

1. What is a class in java?

A class is a group of similar objects which have common properties. It is a

template or a blueprint from which objects are created. It is not a real one.

We can say that class is a picture of an object which comes into our mind.

Class can also be defined as a user defined data type.

A class in java contains:

 fields

 methods

 constructors

 blocks

Syntax to declare a class

class <class name>{

fields;

methods;

2. What is an object?

Object is a real instance of a class. Each object has its own states and

behaviors.

3. What is new keyword in java?

new keyword is used to create an object of the class. It also allocates

memory for it in the heap memory.


4. How many types of memory areas are allocated by JVM?

 Class(Method) Area

 Heap

 Stack

 Program counter register

 Native method stack

5. What if I write static public void instead of public static void for the main()

method?

Program compiles and runs properly. The order of modifiers doesn’t

matter.

6. What is a Constructor in java?

Constructor is used to initialize an object at the time of creation itself.

7. What are the Rules for defining a constructor?

 Constructor name should be same as the class name

 It cannot contain any return type

 All access modifiers are allowed (private , public, protected, default)

 It cannot have any non access modifiers (final ,static, abstract,

synchronized etc)

 It can take any number of parameters

 A constructor can throw exception or we can declare exceptions using

throws for a constructor.

8. What is the use of private constructors in Java?

When we use private for a constructor then object for the class can only be

created internally within the class, no outside class can create object for
this class. Using this we can restrict the caller from creating objects. This

will be helpful in writing a singleton class.

9. Can we have a Constructor in an Interface?

No, we cannot have a constructor defined in an Interface.

10.Can we have a Constructor in an abstract class?

Yes, we can have constructor defined in an abstract class.

11. What is constructor chaining in Java?

Constructor chaining is nothing but calling one constructor from another.

this()is used to call the current class constructor and super()is used to call

the parent class constructor.

Eg:

class A {

public A() {

System.out.print("A");

public A(int i) {

System.out.print("C");

class B extends A {

public B() {

System.out.print("D");

public B(int j) {
System.out.print("B");

public class Test {

public static void main(String[] args) {

B obj = new B(8);

// AB will be printed

By default a constructor will call its super class default constructor. This happens

because of the super() statement in the constructor. Even if we don’t add this

compiler will add this statement. If the super class doesn’t have a default

constructor it will result in a compile time error. We can also call a parameterized

constructor of a super class by calling super() with correct order, type and number

of arguments of the super class constructor.

12. Can we have this() and super() in the same constructor?

No, we cannot have this() and super() in a constructor. Also this() or super()

must be the first statement in the constructor.

13. Is it possible to call a sub class constructor from super class constructor?

No. You cannot call a sub class constructor from a super class constructor.

14. Can we write a class with no constructors in it? What will happen during

object creation?
Yes, we can have a class with no constructor, when the compiler

encounters a class with no constructor then it will automatically create a

default constructor for you.

15. Will compiler create the default constructor when we already have a

constructor defined in the class ?

No, the compiler will not create the default constructor when we already

have a constructor defined.

16. Why constructors cannot be final in Java?

When you set a method as final, then “The method cannot be overridden

by any class”, but constructor by JLS ( Java Language Specification )

definition can’t be overridden. A constructor is not inherited, so there is no

need or doesn’t make sense for declaring it as final.

17. Why constructors cannot be static in Java?

When you set a method as static, it means “The Method belong to class

and not to any particular object” but a constructor is always invoked with

respect to an object, so it makes no sense for a constructor to be static.

18. What is the purpose of default constructor?

Default constructor is used to provide the default values to the object

states like 0, null etc. depending on the type.

19. What is parameterized constructor?

A constructor which has arguments is called parameterized constructor.

20. Does constructor return any value?

No.
21. What happens if you keep a return type for a constructor?

Ideally, constructor must not have a return type. By definition, if a method

has a return type, it’s not a constructor. It will be treated as a normal

method. But compiler gives a warning saying that method has a constructor

name.

22. When do we need constructor overloading?

A class having more than one constructor is called constructor overloading.

Sometimes there is a need of initializing an object in different ways. This

can be done using constructor overloading. Different constructors can do

different work by implementing different line of codes and are called based

on the type and no of parameters passed. So the initialization of objects

can be done in different ways. i.e fully or partially.

According to the situation, one constructor is called with specific number of

parameters among overloaded constructors.

23. Can we define a method with same name of class?

Yes, it is allowed to define a method with same class name. No compile

time error and runtime error is raised, but it is not recommended as per

coding standards.

24. How compiler and JVM can differentiate constructor and method definitions

of both having same class name?

By using return type, if there is a return type it is considered as a method

else it is considered as a constructor.

25.What are the various access specifiers for Java classes?

In Java, access specifiers are the keywords used before a class, method or a

variable name which defines the access scope. The access specifiers are:
1. public : class,method,field is accessible from anywhere.

2. protected: method, field can be accessed from the all the classes in

the same package and also from subclasses in different packages.

3. default: class, method ,field can be accessed only from the same

package and not from outside the package.(If you not use any access

modifiers, it will be treated as default access modifier.)

4. private: method, field can be accessed from the same class to

which they belong. (If a variable is declared as private, we cannot

access it from outside the class. So, we use public methods like

setters and getters for accessing this variable. Such classes are called

fully encapsulated class.)

You might also like