Skip to main content

Posts

Java Exception : Exception Handling in Java

Java Exception :  Exception in Java can be defined as abnormal activity due to unwanted execution of code which should be avoided. But as we know writing too many complex logics can lead to execution of unwanted code and it can disrupt our normal flow.    To avoid such scenarios we have an option option called EXCEPTION HANDLING, by using EXCEPTION HANDLING we can make our execution normal in case of any disruption.   There are 2 ways to handle the exception. If we want the doubtful code which can throw exception and interrupt our execution flow , in this case we can use try and catch block and can do the required things and log the message that this hasn't work and can print the exact failure message with the help of stack trace. Let's understand with an example : below code is without Exception handling which will through Arithmetic exception. package C4CAMPUS ; public class ExceptionExample { public static void main (String args[]){ int a = 10 ; int

OOPS Concept in Java : ENCAPSULATION

OOPS Concept in Java : ENCAPSULATION   This OOPS concept can be used to make things simpler in case of software development . Main purpose of this concept is to hide the properties of any class and give access to fetch and modified based on business criteria.  A simple example can be a POJO ( Plain Old Java Object) in which all the properties of a class can be private and through getter and setter method of properties we can fetch and update the properties of Object. So instead of having direct access to properties we have created 2 methods to make the CLASS things encapsulated in single unit while access to it is via 2 public methods.   Just consider we have requirement that once the object is created its value should not be changed then simplest way to achieve this  can be done by just removing setter method and we will keep only getter methods to access Object properties . In this case after Object creation whatever the value of Object properties has been initialised it will b

OOPS Concept in Java : POLYMORPHISM

 POLYMORPHISM IN JAVA :  Polymorphism means mutiple forms of Single reference. And to understand in simple way just take an example from my previous post of OOPS CONCEPT IN JAVA : INHERITANCE  , I request you guys to go through this link before proceding here. So in this post I have created a method called sum() in PARENT class which has been used by CHILD class without writing same sum() method in CHILD class. This was possible becuase of INHERITANCE concept in java.  But what if CHILD class is not satisfied with PARENT sum() and CHILD class wants to improve it like by adding some message before the calculation. To do this we have another OOPS CONCEPT IN JAVA i.e POLYMORPHISM and by applying this logic we can make same sum() method behvae differently based on Object. As I mentioned earlier POLYMORPHISM means different forms and same has been achieved here by calling same sum() method but the output is different based on Object on which it has been called. If Object is of PARENT

OOPS Concept in Java : ABSTRACTION

  JAVA OOPS CONCEPT ABSTRACTION :  In Java , we need to hide our business logic and actual implementation from others and to achieve this we have 2 options. We can use INTERFACE or ABSTRACT CLASS . Let me tell you the difference between these 2 type of abstraction. INTERFACE should be used when you want 100% abstraction while for partial abstraction we can create  ABSTRACT class.  Let's understand through an example :  if you need to buy a mobile, so you will go to  mobile shop and you will buy a mobile, you won't go to manufacturer company of mobile and ask how they created mobile. right. Same way in Java we will create an implementation class and abstraction to use it without showing actual implementation . For example consider a Calculator class which can do all arithmetic operation like addition , subtraction , multiplication and divide. So if any one wants to use our class  we should not allow to use our class directly, if this is allowed then they can make any kind

OOPS Concept in Java : INHERITANCE

  JAVA OOPS CONCEPT INHERITANCE ( SESSION 2 ) : From the word  INHERITANCE in java only it's clear that this is  related to inheritance of properties and behaviours of a class. So class which is trying to inherit  properties and behaviours of other class is going to called as Child class and the class from where its going to inherit will be the Parent class. Let's consider an example as below. public class PARENT { int a=10; int b=20; public int sum (int fNumber, int sNumber){ int result=fNumber + sNumber; return result;     } } class CHILD extends PARENT{ int a=60; int b=50; public int subtract(int fNumber, int sNumber){ int result=fNumber - sNumber; return result;              } public static void main(String [] args){      PARENT parent=new PARENT();      CHILD child=new CHILD();      System.out.println(parent. sum (parent.a,parent.b));      System.out.println(child. sum (child.a,child.b));      System.out.println(child.subtract(child.a,child.b));         } } Output o

Object-Oriented Programming Concept in Java

OOPS( Object-Oriented Programming ) Concept in Java :   As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.  So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post  BASIC OVERVIEW OF JAVA  (SESSION 1)   Let's understand through an example : public class FirstJavaProgram { int firstNumber=10; int secondNumber=20;      public int sum(int fNum, int sNum){         return fNum+sNum;     }     public static void main(String[] args) {     //our logics     } } In above class , we have define 2 numbers one is holding 10 and another is holdin

Java Program : Writing First Java Factorial Program with explanation

 NAMING CONVENTION IN JAVA : Java is an object oriented programming language , we can relate it to real life object like i mapped Java with human in my previous post JAVA OVERVIEW (SESSION 1)  and represent human properties like body parts as properties in Java and Human can dance , drive , walk , run these can be mapped as Behaviour in java.    Now To represent properties and behaviour in java , there are some standard naming conventions we should follow. Class name should always starts with Uppercase letter like class Student { //Code to be executed } Properties or any kind of variables should starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ; //Code to be executed } Methods name should also starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ;