Object Oriented Programming in Java- Part 2

Q: What are packages and why do we need them ?
A: Provides organization and structure to code. You can have classes with same name in different and avoid collision. Lastly access control and modularity.
Q: How to export import and create a package ?
A: Export => package com.Product.Main;
Import => import com.Product.Main;
// What is the expected output ? assuming Product class exists
public static void main(){
Product eggCurry = new Product();
System.out.println(eggCurry);
}
// com.Kunal.lib:@5939302
// The expected output will be something like this
// wheareas in c++ you will see memory address like 0xff88922
Q: Does java have const keyword ?
A: NO.
Q: What is final keyword before variable mean in java ?
A: It provides immutability. This means that variable cannot be reassigned.
Q: final keyword before class name ?
A: If a class is created using final keyword you CANNOT EXTEND the class. This means you cannot create other subclass from it.
Q: final keyword before function name ?
A: This means this method CANNOT be OVERRIDDEN by sub classes.
// I am assuming Product class exists
// and has attributes String desc and int price
public static void main(){
final int a = 10;
// a = 20 NOT ALLOWED!! because assigning value 20 to a
final Product fish = new Product("tasty fish", 10);
fish.desc = "Big Fish"; // ALLOWED
fish.price = 5;
// fish = new Product("Shark", 200); NOT ALLOWED!!
// bcz you are assiging new Object to fish
}
Q: What is static keyword in java ?
A: static keyword before data member or member function means that it is INDEPENDENT of Objects and instead depends on CLASS Template. Static members are common to all instances.
Q: When are static members created ?
A: They created as soon as the class template is finished.
Q: Can static functions call NON-static functions and members and vice versa ?
A: NO static methods CANNOT call NON-static methods or members because they depend on objects, however, static methods can be executed without objects. Although, NON-static methods can call static methods or members.
Q: What happens when you try to access a method from object ?
A: java compiler first looks up for method inside object, if not found then it checks the class template.
Q: How to access static members ?
A: Static members can be accessed without initializing object - by directly using class name. Furthermore you can access them with using objects too!
Remember STATIC members are instance INDEPENDENT and NON-STATIC members are instance DEPENDENT. Static members are created/resolved at compile time and rest are created at runtime.
public class Main {
public static void main(String[] args) {
System.out.println(Color.green);
Color.printHello();
Color colorOne = new Color();
System.out.println(colorOne.lime);
System.out.println(colorOne.blue);
}
static class Color {
static String blue = "#0000FF";
String lime = "#00FF00"; // Non-static can't access using Color.lime
static String green = "#477f4f";
static void printHello(){
System.out.println("Hello from Color!");
// doSomething(); ERROR!! static method CAN'T Call NON-static methods
}
void doSomething(){
System.out.println("Watching Rocky 1976");
}
}
Q: Why do we write public static void main inside main class to execute our code?
A: You need a main function to run a program. But to execute the function you need to create an object. But how can you create an object without a main function. That's why we write public static void main which means this function is accessible from anywhere and does NOT depend on the Object.
Q: What are the benefits and use cases for static members ?
A: Lets say you want to use specific colors inside your app. You can create a colors class and create an instance everywhere you want to use them. Instead you can initialize static variables so that you can use the colors anywhere you want without initializing an object each time and it will save you lots of space.
Remember If a members ain't static, this means it depends on the outer class and can only be accessed by creating object of outer class. Al
public class Main{
class Car{
String name;
}
public static void main(){
Car benz = new Car(); // ERROR!! why?
// because Car class is dependent on Main class
// So use static keyword before class Car
}
}
Q: What is Singleton pattern ?
A: Imagine a scenario where you want to have just 1 instance of the class available everywhere. This is called singleton pattern.



