Java OOP
How do you fix the java out of memory
1.Allow the JVM to use more memory 2.Improve application to use less memory -Generate a heap dump on the error
What is a class?
A class is a representation of a type of object. It is the blueprint/template that describes the object
What is a constructor?
A constructor is a method that is called when an instance of an object is created. -A constructor does not have a return type -The name of the constructor must be the same name as the class
What are the types of arguments?
A parameters is a variable used during the declaration of a function. Arguments are what is actually passed to the function they should match the parameters defined. Call by value- Value passed will be modified only inside the function and it returns the same value passed into the function Call by reference-Value passed will be modified both inside and outside the function.
What is the difference between a structure and a class?
A structure is used for grouping data whereas class can be used for grouping data and methods.
Abstract class vs interface
Abstract class is used to share code between closely related classes Abstract class contains a mix of methods with or without implementation Abstract class has no limits of modifiers Abstract class has single inheritance Interface is used to share a code skeleton among unrelated classes Interface contains only methods and fields without any implementations Interface fields are public/static/final while methods are public automatically Interface has multiple inheritance.
What are the basic concepts of OOP
Abstraction Encapsulation Inheritance Polymorphism
What is method overriding?
Allows subclasses to provide implementation of a method that is already defined in the main class
What is an abstract class?
An abstract class is a class which cannot be instantiated. Creating of an object is not possible with abstract classes but it can be inherited.
What is an interface?
An interface is a collection of abstract methods. If the class implements inheritance then it inherits all the abstract methods of an interface.
What is the difference between a class and an object?
An object is an instance of a class. Objects hold data but classes do not have any information. The definition of properties and functions can be done at class and can be used by the object. Classes can have sub classes but objects cannot have sub objects
What is an object?
An object is an instance of a class.It has its own state, behavior and identity.
Inheritance
Can be defined as the process where one class acquires the properties such as behavior, methods and fields which was declared in another class. The class which inherits the properties of another is known as a subclass. While the class whose properties are inherited is known as superclass
What is encapsulation?
Encapsulation is wrapping variables and methods together as a single unit. The variables are hidden from other classes. Can be accessed only through the methods of the current class. The levels are: public, private, protected, internal and protected internal
What is the difference between an error and an exception in java?
Error and exceptions are both subclasses of the throwable class Error are mainly caused by the environment that the application is running. Such as when the OutOfMemoryError occurs when the JVM runs of of memory StackOverflowError Exceptions are mainly caused by the application itself. Such as NULLPointerException
What is exception handling?
Exception is an event that occurs during the execution of a program. Exceptions can be of any type: runtime, error. These exceptions can be handled by using exception handling mechanisms like try, catch and throws keyword.
What is function overloading?
If a class has multiple methods having the same name it is called overloading. Overloading can be done by: 1.Changing the number of arguments 2.Changing the data type It increases the readability of the program. void add(int& a, int& b); void add(double& a, double& b);
The issue between equals and hashcodes: If two objects are equal... If two objects have the same hashcode
If two objects are equal they must have the same hashcode. If two objects have the same hashcode they may be equal or may not
Which Object oriented programming concept is used as a reuse mechanism?
Inheritance is OOP concept used as a reuse mechanism
What is ternary operator?
It is almost like a conditional operator takes three arguments. Mostly used to find min and max.
What is an abstraction?
It is an important feature of OOP as it shows only the necessary details to the client of an object. For instance when you go turn on the TV it is not necessary to show all the functions of TV. Whatever is necessary to show the functions of TV will be showed by the abstract class.
Do we require parameters for constructors?
No we do not require parameters for constructors.
What is the difference between overloading and overriding?
Overloading is the same method but with different arguments. Overriding is the same method names with the same arguments and return types are associated with the class and its child class.
What is polymorphism?
Polymorphism is the ability of an object to take many forms. Such as when a parent class reference is used to refer to a child class object. It assigns a value or behavior in a subclass to something that was already declared in the main class.
What operators cannot be overloaded?
Scope resolution(::) Member selection(.) Member selection thru a pointer (.*)
Why is the main method in java static? Why is the main method in java public? Why is the main method in java void?
Since the main method in java is static, the JVM can call it without creating any instance of the class which contains the main method inside. The main method in java is public because Any method or variable which is public in java can be accessed from outside of that class. Since it is public the JVM can access it and execute it. The main method in java is void because it is not supposed to return any value.
What is the super keyword?
Super keyword is used to invoke overridden method which overrides one of its superclass methods. The keyword allows access to overridden methods and also to access hidden members of the superclass.
What is the default access modifier in a class?
The default access modifier of a class is private by default.
What is this pointer?
The this pointer refers to the current object of a class. This keyword is used as a pointer which differentiates between the current object and the global object.
What is a checked exception?
These are the exceptions which are checked at compile time. These must be handled explicitly by either catching or throwing the exception. Use condition 1: Expected but unpreventable (The caller did everything within their power to validate the input parameters, but some condition outside their control has caused the operation to fail. For example, you try reading a file but someone deletes it between the time you check if it exists and the time the read operation begins. By declaring a checked exception, you are telling the caller to anticipate this failure.) Use condition 2: Reasonable to recover from (There is no point telling callers to anticipate exceptions that they cannot recover from. If a user attempts to read from an non-existing file, the caller can prompt them for a new filename. On the other hand, if the method fails due to a programming bug (invalid method arguments or buggy method implementation) there is nothing the application can do to fix the problem in mid-execution. The best it can do is log the problem and wait for the developer to fix it at a later time.)
How can we call the base method without creating an instance?
To create a base method without creating an instance. The method should be: static method and doing inheritance from that class Use base keyword from inherited class
What are tokens?
Tokens are recognized by the compiler and it cannot be broken down into smaller components. These are keywords, identifiers, constants and string literals.
How many instances can be made of an abstract class?
zero instances can be made in an abstract class.