Skip to main content

basics of java in layman term for beginner

Basics of java in layman term for beginner

What is an object ?

object is a real world entity.

Object is an instance of the class.It means something that is alive. Let us take an example of CAR:-

concept of a car is class. I mean we all know that it is having four tyres , one engine , four doors , a seat e.t.c. but we can't do anything just by the concept we need real Car to perform operations on it . This real car is what we call is an object.


Explain about main() method in java ?

main() method is an entry point of a program.
public static void main(String[] args)

Why main() method is public, static and void in java ?

public: It can be accessed from anywhere.
static: no class object need to call this method.
void: it is not returning anything in back.

What is JIT compiler ?

JIT is just in time compiler.JIT compiler runs after the program has started and compiles the code (usually bytecode or some kind of VM instructions) on the fly (or just-in-time, as it's called) into a form that's usually faster,

What is bytecode in java ?

Bytecode is the code that we get after Java file converted into class file( Compile once run anywhere concept is based on this only , we need to convert java code into bytecode or class file after that it can be executed anywhere on any machine)


What is a method in java ?

Method is any function to define the certain operation.

access specifier return type function name parameters
access specifier :public/private/protected
returntype:any data structue
name:any name
parameters:any number of parameters of any type separated by comma.

Example:-
public void getMomoms();
private int noMomos(String yourname);
publib int[] getList(int matches, String[] playernames);

Please refer this code snippet for understanding below concepts:-



package Basics;

public class ParentCar {

 //Declared instances of a class
 int carnumber;
 String carname;
 String cartype;
 
 int speed;
 
 //declaring it as static so that we no need of object calling its value
  String agegroup="18-48";
 
 //Declared Constructor of a class
 public ParentCar(int carnumber, String carname, String cartype) {
  //super keyword is used to call parent constructor
  super();
  //this keyword means current class
  this.carnumber = carnumber;
  this.carname = carname;
  this.cartype = cartype;
 }
 
 //declared function to set the speed
 public void setSpeed()
 {
  this.speed=400;
  System.out.println("speed of a parent car is:"+speed);
 }
 
 
 
 
}


package Basics;

public class ChildCar extends ParentCar{

 String specialFeature;
 
 String agegroup="12-18";

 //constructor of a child car
 public ChildCar(int carnumber, String carname, String cartype, String specialFeature) {
  super(carnumber, carname, cartype);
  this.specialFeature = specialFeature;
 }

 /*
  * Method overwriding
  * Function having same name and return type as of parent class
  * Body is different
  * Also we call it as a Run Time Polymorphism
  */
 public void setSpeed()
 {
  this.speed=260;
  System.out.println("speed of a child car is:"+speed);
 }
 
 public void printAgegroup()
 {
  System.out.println("child class age group"+this.agegroup);
  System.out.println("Parent class age group"+super.agegroup);
 }
 
 //Method overloading method 1
 public void permit(String weekday)
 {
  System.out.println("Ok to drive on"+ weekday);
 }
 
 //Method overloading method 2
 public void permit(String Holiday, String weekend)
 {
  if(weekend.equalsIgnoreCase("saturday"))
  {
   System.out.println("allow on "+weekend+"Holiday ");
  }
  else{
   
   System.out.println("Not allowed on"+weekend+"Holiday ");
  }
 }
 
 public static void main(String[] args)
 {
  //initializing parent class object
  ParentCar pcar=new ParentCar(700, "parent", "Big");
  
  //initializing child class object
  ChildCar ccar=new ChildCar(100, "child", "small", "children functions");
  
  //parent car calling its set speed
  pcar.setSpeed();
  //child class calling its own set speed
  ccar.setSpeed();
  
  //object is of parent but instance is of child hence will
  //call child class method
  ParentCar cpcar=new ChildCar(300, "combined car", "both", "fast");
  cpcar.setSpeed();
  
  //super keyword
  
  ccar.printAgegroup();
  
  //method overloading
  
  ccar.permit("Monday");
  ccar.permit("YES", "Saturday");
  ccar.permit("YES", "Sunday");
  ccar.permit("YES", "Weekday");
 }

}


What is a constructor in java ?

Constructor is a method having the same name as that of a class and is not having any return type.

Constructor can have parameters but one should be must without parameters.

Constructor is used to creating an object of a class.

Child class can call it super class constructor using super.



How to call one constructor from the other constructor ?

using a super keyword.

What is method overriding in java ?

Method Overriding :- It means changing a definition of a method at Run time. This concept we can understand by Inheritance:-

ParentCar is a super class.
ChildCar is a child class.

setSpeed is a method we are overriding. Depending on the instance and situations body will change for the same method of initializing speed.

What is method overloading in java ?

Method overloading is nothing but having the same name but different parameter or different returnType.

Basically name will be same but things will change depending upon parameters and return type.It is also called compile type polymorphism as we know which method to call at runtime.

What is a super keyword in java and difference between this() and super() in java ?

As we have used super() keyword in our last example to call the things that exist in the parent class.To be clearer :-

We have declared the same variable name in both parent and child and will see what calls what?

printAgegroup() calling both time agegroup only but depending upon this or super it is changing value.


Difference between method overloading and method overriding in java ?

Overloading: Compile time polymorphism same name but different signature.
Overwriding: Run time polymorphism same name as well as same signature but body is different.





Comments

.

Popular posts from this blog

Basics of System Design

This article is first one from the series of articles dedicated to system design interviews. Here i am going to present the base scenario to consider before starting to solve system design problems.
Questions to ask?
1) what is the number of requests a website will recieve in a day/month/second? 2) what is the amount of memory a website will deal in a day/month/second? 3) what is the number of servers that can accomodate these requests?
To answer this , first we need to remember the below numbers:-
1 million = 10 lakh = 1000000 = 10^6 1 billion = 1000 million = 10^9
1 KB = 1024 B = 10^31 MB= 10^6 = 1024 KB 1 GB= 10^9 = 1024 MB1 TB = 10^12 = 1024 GB
Memory we need to see in BytesRequests we need to see in numbers
example :-
suppose a website recieves 100M requests every month then:-
requests per day = request per month /24 = 416700 requests requests per second = requests per day / (24*3600) = 4.8 requests per second
memory:-
if we take 20:80 principal where 20 percent is write and 80 percent is …

Calculating size of User and Cache storage

user storage:-
it can be solved using same scenario as mentioned in first article .

https://tech.nazarmubeenworks.com/2019/08/basics-of-system-design-chapter-0.html


Suppose there are 12 Million of users are adding every year meaning 1 Million per month.
So if we consider 5 year there will be around 60 million of users. Now in terms of data we can look to our DB table and get information about it. A user will be generally having name , id , address , some forien keys .
Lets assume 10 columns with each column on an average storing 4 byte of data.
10*4 = 40 bytes of data for one user.
60 * 10^6 * 40 = 2400 * 10^6 = 2.4 * 10^9 = 2.4 GB of data we need to store only user values.
Also while calculating storage for the user there is also one important point we need to remember is of ids. If we are going to have 60 Million of users which means 60*10^6 users so unique id’s will be
as we know below figures are almost equal
2^10=10^3 then 60*(10^3)^2 = 2^20 *2^6
which means we almost need 26 …