Java Flashcards
What is Java Development Kit & what does it include?
In order to start writing the code in Java, we need to install Java Dev. Kit
JDK includes:
- Compiler - will compile for Java a source code into java-byte code, which isn’t unique machine code & will be executed by Java Virtual Machine (JVM)
- Java Runtime Environment (JRE) contains JVM & core java libraries
- Tools for Java development (archiver, docs generator, etc.)
What is a method?
A method is a set of codes which is referred to by name & runs when it’s called. Each method has its own name. We need method to reuse the code: define the code once & use it many times
What is a main method?
Main method is a special method that actually runs the program. Everything in the main method will be executed when we run the program from top to bottom
What is a constructor?
It’s a special method to create an object
What’s a default constructor?
If we don’t define any constructors by ourselves java will provide one default (empty) constructor.
What is the difference between the constructor & the method?
- Constructor doesn’t have a return type & its name must be the same as a class name
- Java provides the default constructor if we don’t define it
- Constructor isn’t inherited by child class
- Method has a return & its name may or may not be the same as a class name
- Method isn’t provided buy Java
- Methods are inherited by child class
What is the difference between local & instance variables?
- Local variables are declared in methods, constructors, or blocks.
- Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.
- Access modifiers cannot be used for local variables.
- Local variables are visible only within the declared method, constructor, or block.
- Instance variables are declared in a class, but outside a method, constructor or any block.
- Instance variables hold values that must be referenced by more than one method, constructor or block
- Instance variables can be declared in class level before or after use.
- Access modifiers can be given for instance variables.
What are the OOP concepts in Java you know?
Object Oriented Programming. In Java, we have 4 concepts:
Encapsulation, Inheritance, Abstraction, Polymorphism.
What’s an encapsulation in java and how do we achieve it?
It’s data protection(hiding) mechanism from the client code. We make instance variables as private and we provide public getters and setters. use pojo class
What’s inheritance in java?
Can you give an example of using the inheritance in your project?
It is a process where one class can inherit visible properties and methods from another class. Parent-child relationship (super class - subclass relationship). Inheritance is used for not creating the object. Using extends keyword
What’s abstraction in java?
When we focus on what object does instead of how it does it by hidding implementation details. We achvieve abstraction by using abstract method.
What’s polymorphism in java?
Polymorphism is the ability of the object to take many different forms.
List list = new ArrayList<>();
list = newLinkedList<>();
What’s POJO?
Plain Old Java Object. Usually, encapsulated objects referred as POJO.
What’s super class?
It’s parent class in inheritance.
What’s the class which every class will inherit from? Why?
java.lang.Object. To give generic behaviors for every object. For example, equals, hashCode, toString.
How many classes we can extend at once?
Just one because java supports only single type inheritance.
*** Java doesn’t support multiple inheritance.
Why Java doesn’t support multiple inheritance?
Java doesn’t support multiple inheritance in classes because of “Diamond Problem”.
Due to this Java does not support multiple inheritance i.e., you cannot extend more than one other class
What’s the difference between method overriding and method overloading?
- Overloading is when we have multiple methods with the same name but different arguments
- Overriding is when we override the parent class or interface method in the child class.
- Most of the method signatures should be the same
- Child class access modifier should be the same or more visible
- Return type should be the same or covariant
- Child class cannot declare bigger exception than parent class
What does ‘this’ and ‘super’ keyword mean in java?
- ‘super’ is used to access the parent class properties and methods.
- ’ this’ is used to access current object properties and methods.
Access modifiers
- public - the member accessible from anywhere in the project
- protected - accessible within the same package & inside subclasses
- default - accessible within the same package only
- private - accessible within the same class only
Method Arguments
Arguments are used to provide the data to the method.
Methods can have multiple arguments with different types
What is variable?
Variable is a container that can hold piece of information. There are different type of variables for different data types in Java.
What kind of logical operators do you know? And how do they work?
Logical operators are used to check whether an expression is true or false. They are used in decision making.
&& (Logical AND) expression1 && expression2
true only if both expression1 and expression2 are true
|| (Logical OR) expression1 || expression2
true if either expression1 or expression2 is true
! (Logical NOT) !expression
true if expression is false and vice versa
What’s a static keyword in Java?
- Static variables and methods belong to the class, not to a specific object.
- We don’t need to create an object to call them, we use them by class name.
public FileHelper {
public static String path = “ABC”;
public static String readContent() {
…
return