Polymorphism

Polymorphism

Basically, 'Poly' means many and 'Morph' means behavior or forms so many behaviors in programming. same functionality are same object which has many behaviors.



Example Code for Ploymorphism Concept

class DemoOverload{

    public int add(int x, int y){  //method 1

    return x+y;

    }

    public int add(int x, int y, int z){ //method 2

    return x+y+z;

    }

    public int add(double x, int y){ //method 3

    return (int)x+y;

    }

    public int add(int x, double y){ //method 4

    return x+(int)y;

    }

}

class Test{

    public static void main(String[] args){

    DemoOverload demo=new DemoOverload();

    System.out.println(demo.add(2,3));     //method 1 called

    System.out.println(demo.add(2,3,4));   //method 2 called

    System.out.println(demo.add(2,3.4));   //method 4 called

    System.out.println(demo.add(2.5,3));   //method 3 called

    }

}



There are two common ways of implementing Polymorphism.
1. Overloading
2. Overriding

Overloading
Using the same method name with different parameter types lists.

Overriding
Using the different implementations of the same method in sub classes.

Abstraction

Abstraction

Normally abstract means eliminate, here show only the necessary details and hide other details. abstract also is a keyword in java.
Eg:- In this picture, we can access the iPhone but we didn't know about the inside functions , circuit  and other things. like wise in the abstract concept it's show the appropriate details and hide the inappropriate details.



Example code for the Abstraction Code

    abstract class MobilePhone
    {
        public void Calling();
        public void SendSMS();
    }

    public class Nokia1400 : MobilePhone
    {

    }

    public class Nokia2700 : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
    }

    public class BlackBerry : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
        public void Recording();
        public void ReadAndSendEmails();

    }




Encapsulation

Encapsulation

 This one of the basic concept of OOP. this is approach to make security to the class, such as private or public. so in this encapsulation it can hide the fields in the class..




Example code for the Encapsulation Concept

class Demo
{
   private int _mark;

   public int Mark
   {
     get { return _mark; }
     set { if (_mark > 0) _mark = valueelse _mark = 0; }
   }
 }


Inheritance



Inheritance

In the programming language, It is derive from the parent class to other sub classes. here we can easily reuse the codes in programming. normally "extends","implements" keywords are used in the programming.





This class is inheriting the data and behaviors from one class to another class.

Vehicle---------------->Car
Car------------------->Speed Car

Example code for the Inheritance Concept


public class ParentClass
    {
        public ParentClass()
        {
            Console.WriteLine("Parent Constructor.");
        }

        public void print()
        {
            Console.WriteLine("I'm a Parent Class.");
        }
    }

    public class ChildClass : ParentClass
    {
        public ChildClass()
        {
            Console.WriteLine("Child Constructor.");
        }

        public static void Main()
        {
            ChildClass child = new ChildClass();

            child.print();
        }
    }



Output:
    Parent Constructor.
    Child Constructor.
    I'm a Parent Class.




Encapsulation

 This one of the basic concept of OOP. this is approach to make security to the class, such as private or public. so in this encapsulation it can hide the fields in the class..

Example code for the Encapsulation Concept

class Demo
{
   private int _mark;

   public int Mark
   {
     get { return _mark; }
     set { if (_mark > 0) _mark = valueelse _mark = 0; }
   }
 }




Abstraction

Normally abstract means eliminate, here show only the necessary details and hide other details. abstract also is a keyword in java.
Eg:- In this picture, we can access the iPhone but we didn't know about the inside functions , circuit  and other things. like wise in the abstract concept it's show the appropriate details and hide the inappropriate details.

Example code for the Abstraction Code

    abstract class MobilePhone
    {
        public void Calling();
        public void SendSMS();
    }

    public class Nokia1400 : MobilePhone
    {

    }

    public class Nokia2700 : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
    }

    public class BlackBerry : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
        public void Recording();
        public void ReadAndSendEmails();

    }





Polymorphism

Basically, poly means many and morph means behavior or forms so many behaviors in programming. same functionality are same object which has many behaviors.

Example Code for Ploymorphism Concept

class DemoOverload{

    public int add(int x, int y){  //method 1

    return x+y;

    }

    public int add(int x, int y, int z){ //method 2

    return x+y+z;

    }

    public int add(double x, int y){ //method 3

    return (int)x+y;

    }

    public int add(int x, double y){ //method 4

    return x+(int)y;

    }

}

class Test{

    public static void main(String[] args){

    DemoOverload demo=new DemoOverload();

    System.out.println(demo.add(2,3));      //method 1 called

    System.out.println(demo.add(2,3,4));    //method 2 called

    System.out.println(demo.add(2,3.4));    //method 4 called

    System.out.println(demo.add(2.5,3));    //method 3 called

    }

}


There are two common ways of implementing polymorphism.
1. Overloading
2. Overriding

Overloading
Using the same method name with different parameter types lists.

Overriding
Using the different implementations of the same method in sub classes.


Java Keywords

These are the important keywords in java programming language.

Keywords are special reserved words in java .They have meaning to the compiler, it uses them to understand what your source code is trying to do.

1. abstract
     It is an class in java programming. it may be or not include the abstract methods.
     eg :- 

    public abstract class GraphicObject {
   // declare fields
   // declare nonabstract methods
   abstract void draw();
}


2.  assert
     This assert is used to take an assumptions in that code to fix bugs. it's a Boolean condition. 
  Simple Syntax 
assert expression1 : expression2;


3. boolean
    This boolean keyword is used for declare two possible values, such as True / False

private boolean initialized = false;

public void synchronizeConnection() {
if (!initialized) {
      connection = connect();
      initialized = true;
}
}

4. break
   This keyword is used for to break the loop
 eg :-
for ( int i=0; i < maxLoopIter; i++ ) {
   System.out.println("Iter=" +i);
   if ( i == 5 ) {
      break;  // -- 5 iteration is enough --
   }
}

5. byte
  It is 8bit integer data type.
eg:-  Basic Syntax 
byte = ;

byte a=10;

6. case
this keyword is used for the decision making or control the flows of program.
eg:- public class SwitchDemo {
    public static void main(String[] args) {

        int month = 2;
        String monthString;
        switch (month) {
            case 1:  monthString = "January";
                     break;
            case 2:  monthString = "February";
                     break;
            case 3:  monthString = "March";
                     break;
            case 4:  monthString = "April";
                     break;
            case 5:  monthString = "May";
             
        }
        System.out.println(monthString);
    }
}
7. catch
   This catch keyword is used for the java exception handling  and used with 'try'.
eg:- try {
   //...
     throw new MyException_1();
   //...
} catch ( MyException_1 e ) {
   // --- Handle the Exception_1 here --
} catch ( MyException_2 e ) {
   // --- Handle the Exception_2 here --
}

8. char
this char is about the characters.this keyword is used for the character variables.

9. class
keyword class to declare that a new class is being defined.

10. const
Const  has no function.[For defining constants in java, see the 'final' reserved word.

11. continue
this keyword is used for to re-execute the conditions such as while,do loop.
eg:- while (getNext(line)) {
  if (line.isEmpty() || line.isComment())
    continue;
  // More code here
}
12. default
this keyword is enable the interface functionality of the library.
13. do
The do keyword is used in conjunction with while to create a do-while loop.
do {
     statement(s)
} while (expression);

14. double
  This keyword is used to declare a numeric value
public class DoubleDemo { 
  public static void main(String[] args) { 
  double area = 6.15;
  System.out.print("Area: ");
  System.out.print(area);
  }
}


15. else
It  is used for compare the boolean values in java programming
void applyBrakes() {
    if (isMoving) {
        currentSpeed--;
    } else {
        System.err.println("The bicycle has already stopped!");
    } 
}

16. enum
       this enum keyword is for predefined the constants.
public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}

17. extends
it can use for the many classes in java language.
public class Bicycle {
        
    // the Bicycle class has three fields
    public int cadence;
    public int gear;
    public int speed;
        
    // the Bicycle class has one constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }
        
    // the Bicycle class has four methods
    public void setCadence(int newValue) {
        cadence = newValue;
    }
}
18. final
Define an entity once that cannot be changed nor derived from later. More specifically: a final class cannot be subclassed, a final method cannot be overridden, and a final variable can occur at most once as a left-hand expression
19. finally
this keyword is execute the try blocks in java
eg:- finally {
    if (out != null) { 
        System.out.println("Closing PrintWriter");
        out.close(); 
    } else { 
        System.out.println("PrintWriter not open");
    } 
} 


20. float
The float  keyword is used to declare a variable that can hold a 32-bit single precision IEEE 754 floating-point number
float b = 3.6f;
21. for
The for keyword is used to create a for loop, which specifies a variable initialization, a boolean expression, and an incrementation

22. goto
It has no value but it's a reserved word.
IF condition THEN goto label

The if keyword is used to create an if statement, which tests a boolean expression; if the expression evaluates to true, the block of statements associated with the if statement is executed.

24. implements
Included in a class declaration to specify one or more interfaces that are implemented by the current class

25. import
normally import is used by the compiler to identify the class.
eg:- import.java.util.Scanner

26. instanceof
binary operator that takes an object reference as its first operand and a class or interface as its second operand and produces a boolean result

27. int
this is primitive data   type in java language. it is a integer.
eg:- // The number 26, in decimal
int decVal = 26;
//  The number 26, in hexadecimal
int hexVal = 0x1a;
// The number 26, in binary
int binVal = 0b11010;

28. interface
Used to declare a special type of class that only contains abstract methods, constant (static final) fields and static interfaces
interface Bicycle {

    //  wheel revolutions per minute
    void changeCadence(int newValue);

    void changeGear(int newValue);

    void speedUp(int increment);

    void applyBrakes(int decrement);
}
29. long
The long keyword is used to declare a variable that can hold a 64-bit signed two's complement integer

30. native
Used in method declarations to specify that the method is not implemented in the same Java source file, but rather in another language

31. new
Used to create an instance of a class or array object.

32. package
A group of types. Packages are declared with the package keyword.

33. private
The private keyword is used in the declaration of a method, field, or inner class.

34. protected
The protected keyword is used in the declaration of a method, field, or inner class; protected members can only be accessed by members of their own class, that class's subclasses or classes from the same package

35. public
The public keyword is used in the declaration of a class, method, or field; public classes, methods, and fields can be accessed by the members of any class

36. return
Used to finish the execution of a method. It can be followed by a value required by the method definition that is returned to the caller

37. short
The short keyword is used to declare a field that can hold a 16-bit signed two's complement integer
short length = 20120;
public short getLength() {
    return 20120;

}

38. static
Used to declare a field, method, or inner class as a class field. Classes maintain one copy of class fields regardless of how many instances exist of that class.
class Student8{
 int rollno;
String name; 
static String college ="ITS"
Student8(int r,String n){
rollno = r;
name = n; 
}
39. strictfp
A Java keyword used to restrict the precision and rounding of floating point calculations to ensure portability

40. super
Used to access members of a class inherited by the class in which it appears. Allows a subclass to access overridden methods and hidden members of its superclass.

41. switch
The switch keyword is used in conjunction with case and default to create a switch statement, which evaluates a variable, matches its value to a specific case , and executes the block of statements associated with that case

42. synchronized
Used in the declaration of a method or code block to acquire the mutex lock for an object while the current thread executes the code

43. this
Used to represent an instance of the class in which it appears. this can be used to access class members and as a reference to the current instance

44. throw
45. throws
Used in method declarations to specify which exceptions are not handled within the method but rather passed to the next higher level of the program.
46. transient
47. try
Defines a block of statements that have exception handling. If an exception is thrown inside the try block, an optional catch block can handle declared exception types. 

48. void
The void keyword is used to declare that a method does not return any value
49. volatile
Methods, classes and interfaces thus cannot be declaredvolatile, nor can local variables or parameters.
50. while
The while keyword is used to create a while loop, which tests a boolean expression and executes the block of statements associated with the loop if the expression evaluates to true, this continues until the expression evaluates to false.