The significance of exception handling comes here in order not to terminate a program abruptly and to continue with the rest of the program normally. When an exception is raised in the program it leads to the termination of the program when it is not handled properly. Objects created are stored in heap memory and reference to those objects are stored in stack memory.Įxception handling is a mechanism for what to do when some abnormal situation arises in the program. No, We can not create the object of a static class. The compiler automatically adds the default constructor in every class whether it is an abstract class or a concrete classġ3)Can we create the object of abstract classes? An abstract class can have instance variables and non-abstract methods so to initialize those we may explicitly define a constructor in an abstract class. The constructor is used to initialize the newly created object or fields of a class. An abstract class is used to achieve partial abstraction while an interface is used to achieve full abstraction.ġ2)Why does an abstract class needs a constructor?.An abstract class can contain constructors while Interface can not have constructors.An interface can only have abstract methods while an Abstract class can have non-abstract methods along with abstract methods.An abstract class can have instance variables while Interface can not have instance variables.The use of an interface allows us to achieve multiple inheritance and complete abstraction.ġ1)What is the difference between abstract class and interface? Interfaces are a way to declare a contract for implementing classes to fulfill. Interface in java is used to achieve code reusability and polymorphism. Abstraction hides the underlying complexity of data and Gives flexibility to programmers to change the implementation of abstract behavior Encapsulation exposes only parts of the object which are safe to expose and the remaining part of the object is kept secure.Ībstraction aims to hide complexity from users and show them only relevant information. If the string were somehow not immutable, then changing one reference would alter the value of other references.Įncapsulation is the process of wrapping code and behavior in a single unit called class and preventing misuse is called encapsulation. Making a string immutable makes the string thread-safe. Primitive data types are stored in the stack while derived objects are stored in the heap and their reference in the stack.Ħ)What are the different ways to create objects in java? Primitive data types are predefined by the programming language such as int, float, char, and boolean while non-primitive created by programmers like an array, enum, etc. Constructor chaining is also used to access the properties of the constructor in the same class or in another class.ĥ)What is the difference between primitive and non-primitive data types?Ī primitive type has always a value while non-primitives can be null. By using constructor chaining code optimization can be achieved. As and when an object is created it is initialized automatically with the help of a constructor in java.Ĭonstructor chaining is the process of calling one constructor from another constructor. We use constructors to initialize all variables in the class when an object is created. Either this() or super() exists it must be the first statement in the constructor. This() is used to access one constructor from another within the same class while super() is used to access the superclass constructor. Protected: Access level is within the package and outside the package through child class.ĭefault: Access level is only within the package.Ģ)What is the difference between this() and super() in java? Public: It can be accessed from anywhere like within the class, outside the class, outside the package, etc. Private: Access is limited within the class. This post on JAVA Interview Questions is intended to help you understand the fundamental concepts of Java for interview purposes.ġ)Explain access modifiers and their use in java.Īccess modifiers help in restricting the scope of the class, constructor, variable, data member, or method.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |