day 1 (1)
day 1 (1)
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.
fields
methods
constructors
blocks
fields;
methods;
2. What is an object?
Object is a real instance of a class. Each object has its own states and
behaviors.
Class(Method) Area
Heap
Stack
5. What if I write static public void instead of public static void for the main()
method?
matter.
synchronized etc)
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
this()is used to call the current class constructor and super()is used to call
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");
// 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
No, we cannot have this() and super() in a constructor. Also this() or super()
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
15. Will compiler create the default constructor when we already have a
No, the compiler will not create the default constructor when we already
When you set a method as final, then “The method cannot be overridden
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
No.
21. What happens if you keep a return type for a constructor?
method. But compiler gives a warning saying that method has a constructor
name.
different work by implementing different line of codes and are called based
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
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
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
access it from outside the class. So, we use public methods like
setters and getters for accessing this variable. Such classes are called