How does the Object Oriented approach improve software development?

The key benefits are: Re-use of previous work: using implementation inheritance and object composition. Real mapping to the problem domain: Objects map to real world and represent vehicles, customers, products etc: with encapsulation. Modular Architecture: Objects, systems, frameworks etc are the building blocks of larger systems. The increased quality and… Continue reading

What are the advantages of Object Oriented Programming Languages (OOPL)?

The Object Oriented Programming Languages directly represent the real life objects like Car, Jeep, Account, Customer etc. The features of the OO programming languages like polymorphism, inheritance and encapsulation make it powerful. [Tip: remember pie which, stands for Polymorphism, Inheritance and Encapsulation are the 3 pillars of OOPL]

How to call the superclass constructor?

If a class called “SpecialPet” extends your “Pet” class then you can use the keyword “super” to invoke the superclass’s constructor. E.g. public SpecialPet(int id) { super(id); //must be the very first statement in the constructor. } To call a regular method in the super class use: “super.myMethod( );”. This… Continue reading

What happens if you do not provide a constructor?

Java does not actually require an explicit constructor in the class description. If you do not include a constructor, the Java compiler will create a default constructor in the byte code with an empty argument. This default constructor is equivalent to the explicit “Pet(){}”. If a class includes one or… Continue reading

What is the difference between constructors and other regular methods? What happens if you do not provide a constructor? Can you call one constructor from another? How do you call the superclass’s constructor?

Constructors Regular methods Constructors must have the same name as the class name and cannot return a value. The constructors are called only once per creation of an object while regular methods can be called many times. E.g. for a Pet.class  public Pet() {} // constructor Regular methods can have… Continue reading

What are “static initializers” or “static blocks with no function names”?

When a class is loaded, all blocks that are declared static and don’t have function name (i.e. static initializers) are executed even before the constructors are executed. As the name suggests they are typically used to initialize static fields. public class StaticInitializer { public static final int A = 5;… Continue reading

What do you need to do to run a class with a main() method in a package?

Say, you have a class named “Pet” in a project folder “c:\myProject” and package named com.xyz.client, will you be able to compile and run it as it is? package com.xyz.client; public class Pet { public static void main(String[] args) { System.out.println(“I am found in the classpath”); } } To run:… Continue reading

Explain Java class loaders? If you have a class in a package, what do you need to do to run it? Explain dynamic class loading?

Class loaders are hierarchical. Classes are introduced into the JVM as they are referenced by name in a class that is already running in the JVM. So, how is the very first class loaded? The very first class is especially loaded with the help of static main( ) method declared… Continue reading