Posts

Showing posts from April, 2023

OOP IN JAVA - [ ENCAPSULATION & this KEYWORD]

Image
  ENCAPSULATION  Encapsulation  is one of the last  fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction .  In Java, encapsulation is the technique of combining code and data into a single entity. Java allows us to entirely encapsulate a class by keeping all of its data members private. We can now set and retrieve data from it using setter and getter functions. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as  data hiding . E ncapsulation in Java − Declare the variables of a class as private. Ex -:                    class School                               private int index_ num;                 ...

OOP IN JAVA - (TYPES OF VARIABLE | TYPES OF METHOD)

Image
  Types of variables  In Java mainly contain 3 types of variables. LOCAL VARIABLE  Variables   are   declared   in   a  body  way   called   local   variables.   You   can   use   variables   only   in   this   method   and   not   even   other   methods   in   the   class. ex -  Instance  V ariable   Variables   declared   inside   a   class   but   outside   the   body   of   a   method   are   called   instance   variables.   It   is   not   declared   as   static.                                                                           ...

OOP IN JAVA -( CONDITIONS - if | else | else if)

Image
java in mainly contain three types of Conditions if Condition else Condition else if Condition   If  Condition  The condition after evaluation of if-statement will be either true or false. And also the if statement in Java accepts Boolean values and if the value is true then it will execute the block of statements under it. important Note ;   If we do not provide the curly braces ‘{‘ and ‘}’ after if( condition ) then by default if statement will consider the immediate one statement to be inside its block.  Ex :-                                           public Class IFstatment{                                   public static void main (string [] args)  {                           ...

OOP IN JAVA - LOOP [ while loop AND for loop ]

Image
  LOOPS L oops can execute a block of code as long as a specified condition is reached and  also they  are handy because they save time, reduce errors, and they make code more readable. there are two ways of loop                1. While Loop               2. For Loop while Loop  The while loop is considered as a repeating if statement. If the number of iteration is not fixed, it is recommended to use the while  loop .  The  while  loop loops through a block of code as long as a specified condition is  true . syntax                                while (condition) {                               // code block to be executed              ...

OOP IN JAVA -( INTERFACE - "Implement" KEYWORD)

Image
  INTERFACE   A n interface is a concept like a class that can be used to inherit because there is no way to do multiple inheritance in java, but it is not a class.                                         WHY USE INTERFACE  It is used to achieve abstraction. By interface, we can support the functionality of multiple inheritance. It can be used to achieve loose coupling NOTE :   Implements keyword is used in interface just like extend in class interfaces  cannot  be used to create objects and other thing is an interface cannot contain a constructor (as it cannot be used to create objects)          Ex - interface School{          }               Animal a = new Animal(); // Total Wrong creation method            ...

OOP IN JAVA - ( "abstract" KEYWORD)

Image
    A BSTRACTION  As Java is an OOP language, abstraction can be seen as one of the important features and building blocks of the Java language. In Java, abstraction is implemented using an abstract class and interface and another explanation " is a process of hiding the implementation details and showing only functionality to the user." first of all I would to explain  : What is abstract class ? An abstract class can be defined as a class declared with the keyword “abstract” and has a restriction that it cannot be instantiated. What is abstract Method? A method that does not have a body is called an abstract method in Java. It is declared with the modifier abstract.  and especial thing is t he following are the properties of the java abstract method, can only be used in an abstract class, and it does not have a body. An abstract method contains a method signature, but no method body. An abstract method is a method that is declared without implementati...

OOP IN JAVA : (POLYMORPHISM)

Image
  Polymorphism P olymorphism is one of the core concepts in OOP languages and describes the concept wherein you can use different classes with the same interface. Each of these classes can provide its own implementation of the interface. A fter overriding a method of a super class in a sub class, casting it and making the sub class method run through the super class reference. Using overriding inheritance casting concept 3 to call the sub class from the super class reference. Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object,  for the Example :- class Person { public void teach(){ System.out.println("Person can teach"); } } class Teacher extends Person { public void teach() { System.out.println ("Teacher can teach in a school"); } } public class TestTeacher { public static void main(String args[]) { ...

OOP IN JAVA ( UPCASTING & DOWNCASTING)

Image
  UPCASTING AND DOWNCASTING  Up casting :  Basically ,Upcasting gives us the flexibility to access the super class members but it is not possible to access all the sub class members using this feature. And also we use upcasting to combine something in the super Class (Objects, Variable and methods) with (objects, Variable and methods) of the sub class.     class A{   } class B extends  A{ } class Test{ A a = new A(); B b = new B(); } b= a ( Totally Wrong) a=b ( correct) How to create Upcasting Method Process ;                                              A a = new B ()    A - Super class Object Reference     a - Reference variable   B - Sub class Object Reference     Note : in upcasting, you can't access from lower power to up power. you can access from up powe...