Object Oriented Programming in Java - Part 1

Q: What are classes ?
A: Classes are logical construct AKA Blueprint. Similar to blueprint/Architecture/Map of a house or car etc.
Q: What are Objects ?
A: Objects are physical reality AKA living entities. Analogous to ACTUAL car or House.
Q: What happens when you execute your Java program?
A: When u click run, first your code is Compiled and then Runtime execution takes place.
Q: What is compilation process?
A: compiler checks for syntactic errors and if no error is found the Java code is converted in byte code(This is machine independent code and can run on any device irrespective of the OS/Hardware). Java compiler generated byte-code for each class.
Q: What is Runtime execution?
A: Now JVM executes the byte-code. It loads the byte-code in memory, initializes the classes and the "main" function is executed.
Q: What is Dynamic Memory Allocation ?
A: Memory which is allocated during runtime execution.
Q: What is stack memory ?
A: Stack memory contains local variables, function calls (order in which fun are called), temp data. Stack memory is allocated during compile time.
Q: What is heap memory ?
A: Heap memory is dynamically allocated during runtime using the "new" keyword. This means memory is allocated when JVM is executing the byte-code.
Q: Why do we need constructors?
A: Constructors helps to initialize the object attributes. Helps to "BIND" the data with the object.
Q: Things one should know about constructors?
A: Constructors have the same name as that of the class. It has no return type. It is automatically executed as soon as the object is created.
Q: What is "this" keyword and its usage
A: "this" keyword refers to the reference variable. Imagine a scenario when you want to assign values to the class objects but the constructor parameters have the same name as that of class attributes. This results in confusion.
This is why we use this keyword OR you can have constructor parameters with different name.
// -----------------------------ERROR----------------------------------------
public class Main {
public static void main(String[] args) {
Product fish = new Product("Fish", 10, 4.2f);
}
static class Product {
String desc;
int price;
float rating;
Product(String desc, int price, float rating) {
// Compiler is confused what to do here!
desc = desc;
price = price;
rating = rating;
}
}
}
//--------------------------------Correct Usage-----------------------------
public class Main {
public static void main(String[] args) {
Product fish = new Product("Fish", 10, 4.2f);
}
static class Product{
String desc;
int price;
float rating;
Product(String desc, int price, float rating){
this.desc = desc;
this.price = price;
this.rating = rating;
}
}
}
Q: Types of Constructors
A: 1. Default constructor. 2. Parameterized constructor. 3. Copy constructor.
Q: What is default constructor ?
A: Constructor which has NO PARAMETERS and comes by default when you create a class. Default constructor initializes every value to 0 , null, "" etc. Note just because you don't see a constructor doesn't mean it doesn't exists.
See reference variable "emptyPlate".
Q: What is parameterized constructor?
A: Constructor which has PARAMETERS is called parameterized constructor. See reference variable "paneer"
Q: What is a copy constructor ?
A: Copy constructor is used to "DEEP COPY" the contents of one object to another. See reference variable "paneerTikka".
Q: Diff between shallow copy and deep copy ?
A: Shallow copy means modification to one variable will result in modification to another variable. However deep copy means modification to one will not result in modification to another as complete attributes are copied.
// Example of SHallow Copy
public static void main(){
Product biryani = new Product("Hyd biryani", 14, 4.1f);
Product food = biryani;
// food points to same Object which biryani points to
// Any changes to food obj will result in changes to biryani obj
}
// Example of Deep Copy
public static void main(){
Product biryani = new Product("Hyd biryani", 14, 4.1f);
Product food = new Product(biryani);
// food is pointing to an obj which has exact same property values as that
// of biryani so any modification to food won't affect biryani variable
}
Q: What is constructor overloading?
A: Analogous to function overloading, constructor overloading means when you more than 1 constructor and depending upon the type of parameters, no of parameters, and ORDER of parameters constructor is called.
public class Main {
public static void main(String[] args) {
Product emptyPlate = new Product(); // line 1
Product paneer = new Product("Tasty paneer", 10, 4.2f); // line 2
Product paneerTikka = new Product(paneer); // line 3
}
static class Product{
String desc;
int price;
float rating;
Product() {
// default constructor
desc = "";
price = 0;
rating = 0.0f;
}
Product(String desc, int price,float rating){
// parameterized constructor
this.desc = desc;
this.price = price;
this.rating = rating;
}
Product(Product something){
// Copy constructor
this.desc = something.desc;
this.price = something.price;
this.rating = something.rating;
}
}
}
Q: Why we don't use new keyword when declaring primitives. Ex int a = new int(10) ?
A: It is because primitives are NOT IMPLEMENTED as OBJECTS. Furthermore primitives are stored in stack and Objects are stored in Heap.
Q: What are wrapper class and why do we need them?
A: Wrapper classes converts primitives to Objects and provides inbuilt functions. Like Integer for int, Boolean for boolean etc.
public static void main(){
int a = 10;
Integer b = 10;
// a.toString() is UNDEFINED behaviour
// b.toString() is allowed
}
Q: Does Java provides call by reference ?
A: NO java doesn't provide call by reference. Everything is call by value. When objects are passed their references are passed, Which gives the illusion of call by reference.



