Day 02:- Object Oriented Programming

DAY 02- Object Oriented Programming

========================================================================
TOPICS

  • Polymorphism
  • Continue With Method Overloading.
  • Creating Function With Return Type
  • Continue With Static And Non Static.
  • What Is An Object
  • Difference Between Object And Object References
  • Constructor
  • Anonymous Object In Java
  • This Keyword
  • Data Shadowing
  • Constructor Chaining
========================================================================
POLYMORPHISM 

All object-oriented programming languages provide mechanisms that help you implement the object-oriented model.They are encapsulation,inheritance,and polymorphism. 

Let’first understand what polymorphism is all about.

Polymorphism in Java means (one name many forms): - it is used to reduce the complexity.

Polymorphism is always achieved by behavior/ functionality. This can’t be achieved using data members.

Types of polymorphism in java 
  • Compile time polymorphism: - Method overloading, Operator overloading (Has a relationship)
  • Run time polymorphism: - Method overriding  (will discusses this in next session) (Is a relation ship)
First lets understand the Compile Time Polymorphism

Method Overloading: In simple terms we can say that a class can have more than one methods with same name but with different number of arguments or different types of arguments or both.

Operator Overloading: Java doesn't support user-defined operator overloading. The only aspect of Java which comes close to "custom" operator overloading is the handling of + for strings, which either results in compile-time concatenation of constants or execution-time concatenation using StringBuilder/StringBuffer Class.

CONTINUE WITH METHOD OVERLOADING

Now lets understand why method overloading is known as compile time binding with below screen print.



Also till now we have achieved method overloading for instance function let talk about static method.

Note:- Static method can be overloaded same as instance method in addition to this main is also a static method which can overloaded.

Let's take a look by below given screen print.



As has been pointed out in below screen print we can't overload a method by changing the primitive return type.




CREATING METHOD WITH RETURN TYPE

We declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value.

Any method declared void doesn't return a value that means It does not need to contain a return statement, but it may do so. In such a case, a return statement can be used to branch out of a control flow block and exit the method.

The data type of the return value must match the method's declared return type; you can't return an integer value from a method declared to return a Boolean.

In below example we are trying to hold the integer value in to byte, thus getting error message.




so the variable receiving the value returned by a method (such as square, in this case) must also be compatible with the return type specified for the method.


Note:- void does not return any value thus we can't hold the value of function having void return type.

CONTINUE WITH STATIC AND NON STATIC STUFF

Now lets understand the scope of static and non static Data Members and Member function.


Note:-

Class Level - Data Members


  • At class level we can declare instance as well as non static Data Members.
  • If data members declared as static then we can accessed static DM in static and instance function directly.
  • If data members declared as instance then we can accessed instance D M in instance function directly but if we want to access the instance data members in static function we have to create an object.


Static And Non-Static Functions - Data Members


  • At function level as we know we can declare the variable and we called them as local variable.
  • Local Variable can't be created as static.
  • We can declare local variable in both static and non static function.
  • The scope of the local variable is limited to that function only in which it is declared.
  • variable declared in any function can be use directly in that function.
  • Local variable are stored in a memory location know as stack.


Note:- we can hold the reference of static in non static variable, but we can't directly hold the reference of non static variable in static.


OBJECTS IN JAVA

In the real world, there are various object around us like Cars, Mobiles, Humans etc and each object is having some states (properties) and behavior (functionality)


Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages).

Real word example



Now lets examine the same thing in software.





Now  as per the class definition we can say a class is a collection of similar type of object and its a blueprint from which objects are created also its logical entity.

OBJECTS AND OBJECT REFERENCES


Now lets take example of Persons class.

Java Standard object creation format.

Person sally = new Person();


So here we can say that we have created an instance of class.

Now object Name i.e. sally is actually an reference variable which  basically holds the address of object which is created using new operator using constructor.



Note:-
  • Object is an Instance of a class.
  • Class is template or blueprint from which individual objects are created.
  • Instantiation means creation of object.
    • Class objName = new Constructor();
  • The constructor will always has the same name as the class.
  • Memory Reference means where the object is stored in the memory.
CONSTRUCTORS IN JAVA

  • Constructor are special type of function which are used to create an object with the help of new operator.
  • Constructors are used to perform initialization of instance variable of the class.
  • Constructor is a special method that is called when you use the new operator.
Now lets understand how can we initialize the instance variable using constructors.



Now lets initialize the variable with help of constructor and use that instance variable via different object.



CONSTRUCTOR OVERLOADING

We can overload a constructor just like any other method, but while calling them we have to create the object with the same parameters.

Example :-

Note:-
  • Name of the constructor should be same as class name.
  • Constructor can have arguments.
  • Java compiler placed a default constructor in class if user does not write any constructor in class.
  • Constructor does not have any return type though it returns the instance of a class.
  • If user provide any constructor, then compiler will not generate default constructor.
  • Constructor are auto executed when we want to execute a particular code whenever we create and object.
  • Constructor are used to initialize instance variable.
  • Constructor chaining will be achieve with the help of this keyword and this should be the first line In the constructor.
  • One constructor is able to call only one constructor at a time.
  • If any object has been created and which do not have any reference then that object is know as Anonymous object.
  • Constructors can't be called explicitly. i.e. with the help of object

THIS KEYWORD IN JAVA

  • "This" hold the reference of current object.
  • "This" keyword can we used in side a instance function or constructors.
  • "This " keyword can't be used in static context.
We can use this keyword for data shadowing and in constructor chaining.

Data Shadowing

Shadowing refers to the practice in Java programming of using two variables with the same name within scope.

suppose we declare a variable in a class as int "x =60" and in the same class we define a method having a local variable same as "int x =80", now in this case if we access the value of x in that method that value will be 80 which means local variable shadow the value of class level variable.

Remember Priority goes to local or you can say that the variable with the higher-level scope is hidden because the variable with lower-level scope. The higher-level variable is then “shadowed.”

Note:- If we don't have duplicate variable in method same as class even though java use this keyword internally to call any instance member function.

Let take an example for instance function:-



Now let use this keyword to remove the shadowing.

Now lets see the second usages of "This" keyword, we can achieve constructor chaining using this keyword.

As we have seen earlier if we have overloaded constructors then to call each parameterised constructor we have to create a object with same parameter but this problem can be resolved with the help of "This" keyword and this concept is known as constructor chaining.

lets see the example to achieve constructor chaining.



Note:- 
  • This must be the first statement if we use this in constructor.
  • We can't use multiple "This" keyword in any constructor.
  • This can be used to refer current class instance variable (Data shadowing).
  • This keyword can be used to invoke current class constructor.
  • Use this keyword we can achieve constructor chaining.
  • We can print the value of This keyword using println function which return the value of current class instance.


========================================================================

Links for the files (.Java File)

https://drive.google.com/drive/folders/0B5M4BcbfVN07LTB4UlJyMHFQU1U?usp=sharing


Links for recorded Video


https://www.youtube.com/watch?v=VxZGzEa36XM


========================================================================




Post a Comment