Java OOP Flashcards
Describe what ‘Public’ means in a user defined method
public - access modifier. It means the method can be accessed from anywhere.
Describe what ‘Static’ means in a user defined method
Static means that the method can be accessed without any objects.
Describe what ‘Void’ means in a user defined method
Void means that the method does not return any value
Describe the complete syntax of a method
modifier static returnType nameOfMethod (parameters) { // method body }
What are some advantages of using methods?
The main advantage is code reusability. We can write a method once, and use it multiple times. We do not have to rewrite the entire code each time. Think of it as, “write once, reuse multiple times
Methods make code more readable and easier to debug
What is a constructor?
In Java, every class has its constructor that is invoked automatically when an object of the class is created. A constructor is similar to a method but in actual, it is not a method.
What are the three types of constructors in Java?
No-Arg Constructor
Default Constructor
Parameterized Constructor
Describe a No-Arg Constructor
A Java constructor may or may not have any parameters (arguments). If a constructor does not accept any parameters, it is known as a no-arg constructor. For example,
private Constructor( ) { // body of constructor }
Describe a default constructor
If you do not create any constructors, the Java compiler will automatically create a no-argument constructor during run-time. This constructor is known as the default constructor. The default constructor initializes any uninitialized instance variables with default values
Describe parametrized constructor
Similar to methods, we can pass parameters to a constructor. Such constructors are known as a parameterized constructor. For example,
private Constructor (arg1, arg2, ..., argn) { // constructor body }
What is constructor overloading?
In constructor overloading, there are two or more constructors with different parameters
Important Notes about Constructors
Constructors are invoked implicitly when you instantiate objects.
The two rules for creating a constructor are:
The name of the constructor should be the same as that of class.
A Java constructor must not have a return type.
What are access modifiers?
In Java, access modifiers are used to set the accessibility (visibility) of classes, interfaces, variables, methods, constructors, data members, and the setter methods. For example,
class Animal { public void method1() {...}
private void method2() {…}
}
There are four access modifiers keywords in Java
Default
Private
Protected
Public
What is a private access modifier
When variables and methods are declared private, they cannot be accessed outside of the class
What is a protected access modifier
When methods and data members are declared protected, we can access them within the same package as well as from subclasses
What is a public access modifier
When methods, variables, classes, and so on are declared public, then we can access them from anywhere. The public access modifier has no scope restriction
Describe ‘this’ keyword
In Java, this keyword is used to refer to the current object inside a method or a constructor
What is the final keyword used for in Java
In Java, the final keyword is used to denote constants. It can be used with variables, methods, and classes
What is a recursive function?
In Java, a method that calls itself is known as a recursive method. And, this process is known as recursion
What is inheritance?
Inheritance is one of the key features of OOP (Object-oriented Programming) that allows us to define a new class from an existing class
Describe ‘is-a’ relationship
Inheritance is an is-a relationship. We use inheritance only if an is-a relationship is present between the two classes.
Here are some examples:
A car is a vehicle.
Orange is a fruit.
A surgeon is a doctor.
A dog is an animal.
Types of inheritance
There are five types of inheritance.
Single inheritance - Class B extends from class A only.
Multilevel inheritance - Class B extends from class A; then class C extends from class B.
Hierarchical inheritance - Class A acts as the superclass for classes B, C, and D.
Multiple inheritance - Class C extends from interfaces A and B.
Hybrid inheritance - Mix of two or more types of inheritance.
Describe abstraction
Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user
Think — a coffee machine. It does a lot of stuff and makes quirky noises under the hood. But all you have to do is put in coffee and press a button
Describe Encapsulation
Encapsulation is achieved when each object keeps its state private, inside a class. Other objects don’t have direct access to this state. Instead, they can only call a list of public functions — called methods
Describe Polymorphism
Polymorphism is a object oriented programming feature that allows us to perform a single action in different ways
What is an interface?
An interface is a blueprint of a class, which can be declared by using interface keyword. Interfaces can contain only constants and abstract methods (methods with only signatures no body).Like abstract classes, Interfaces cannot be instantiated, they can only be implemented by classes or extended by other interfaces. Interface is a common way to achieve full abstraction in Java
What is abstraction?
Hiding internal details and showing functionality is known as abstraction. For example phone call, we don’t know the internal processing
What are the four principles of object oriented programming?
The four principles of OOP are
Encapsulation
Abstraction
Inheritance
Polymorphism
In terms of inheritance, what is the effect of keeping a constructor private?
It has direct implications for inheritance, since a subclass calls its parents constructor. The class A can be inherited, but only by its own or its parents inner classes
In Java, does the finally block get executed if we insert a return statement inside the try block of a try-catch-fianlly?
Yes, it will get executed. The finally block get executed when the try block exits.