Posts

Upcasting and Downcasting (OOP - JAVA)

Image
  Upcasting and Downcasting Upcasting A object, a variable or a method inside a super class is accessed a object, a variable or a method in a sub class called Upcasting.  A a = new B();  super class object reference - A reference variable - a sub class object reference - B eg:   int x = 5; long y = 5; x = y;    <= wrong way y = x;    <=correct way eg:  double a = 7.88; float b = 25.98f; a = b;    <=correct way b = a;    <=wrong way Consider the following example. class A{ } class B extends A{ } class Test{ A a = new A(); B b = new B(); b = a;        <=wrong way a = b;        <=correct way } Downcasting Sub class object which has been assigned to the Super class variable is converting to a sub class object called Downcasting. consider the following example. class A{ } class B extends class A{ } class Test{ A a = new A();    <=correct B b = new B();...

Method Overriding (OOP - JAVA)

Image
  Method Overriding Changing the body of a method which comes from a super class to a sub class called method overriding. The method which is related to the object, run always. Consider the following example. class Monkey{     void climb(){          System.out.println("Using body");     } } class Man extends Monkey{     void climb(){           System.out.println("Using ladder");     } } super class - Monkey sub class - Man   if we create a object using Man class, Man m = new Man(); m.climb(); The output => Using ladder super keyword We use super keyword to call the constructor of super class from a sub class. Always we call the super class from this keyword. We can access the methods and variables inside the super class from a sub class by using super keyword. Check the following example. class Monkey{     Monkey(){     }     void climb(){  ...

Inheritance (OOP - JAVA)

Image
  Inheritance Let's consider the first BMW car and a BMW 740E car. There are lots of differences and similarities in   those cars.  See the similar properties for those cars, tires  engine light door  break liners seats steering wheel    See the similar behaviors for those cars, drive reverse break horn signal lights on/off See the newly properties for  BMW 740E, cameras sensors digital dashboard video player GPS air bags See the newly behaviors for  BMW 740E, auto pilot voice command When we think about the above properties and behaviors, we can see each an every property and behavior which is related to the first BMW car have been joined to the  BMW 740E car. In other words, the first BMW car properties and behaviors have been inherited to the  BMW 740E car. The things which are related to a class goes to another class is called Inheritance. We need to make a joint between two classes to do it. The first class is called as su...

Method Overloading (OOP - JAVA)

Image
  Method Overloading When we create same method continuously in a same class, we use method overloading concept. We can change the parameters, when we performing method overloading. We can say it method signature as well. Consider following code segment,     eg:   class World{                   void countries( int x ){                     }               void countries( String y ){                   }                }    Method signature segment has been painted in gray color.  Consider following code segment, void duneth () { } void duneth (String s) { } void duneth (int x) { } void duneth (int y, int z) { } duneth (3, 87) ; duneth () ; duneth ("Hello World") ; duneth (123) ; If you look ...

Parameters and Arguments, Constructors (OOP - JAVA)

Image
  Parameters and Arguments   Consider following code lines, A a = new A();   << this is the way to create a object as we know int b = 7;  << int is class of Integer class, b is a variable, 7 is the value String str = "duneth";       How to write the main method in java  public static void main(String[] args){ } Methods void myHeight( ){ } A method has a return type and a method name above example: return type - void , method name - myHeight consider following example, void addNumbers( ) {      int a = 2;      int b = 7;      int c = a+b;      System.out.print(c); } ( ) parameter is used for get the input and output values return type is used for return the output or input consider following example, void area(int x, int y){ } area(7,15);  (int x, int y) is a parameter list area(7,15) is a argument list When we add values to the argument list, it will transfer to the...

Object Creation, Data Types (OOP - JAVA)

Image
  OBJECT CREATION In java we can generate objects if only we have a class. We can convert only a class into object class Human{  } creating objects - Human h1 = new Human();                               Human h2 = new Human();                                      Human h3 = new Human(); eg: Human duneth = new Human(); class name of object - Human variable name - duneth object generate keyword - new class name of object with parameters - Human() semi colon -  ; if we consider a name with surname, surname : Anderson name : Michael                Stephanie               Jimmy in that case class is surname and particular object is name. 😇 we can write as below, 1st way         ...

Standards, Specific Operators, Class and Object (OOP - JAVA)

Image
OBJECT ORIENTED PROGRAMMING CONCEPTS IN JAVA Class Hierarchy 1.Object 2.Class 3.Method 4.Variable STANDARDS           We use some standards to write java codes 😇                   class Java {                       String str ;                       public void test() {                  }                  } We use simple letter to start variable names. We use camel case to write variable names.  (yellow) We use capital letters to start class names. We use mixed case to write class names. (purple) We use simple letters to start method names. We use camel case to write method names. (blue) eg: Duneth - class        duneth() - method       duneth - variable Java print statement...