Saturday, September 7, 2019

Polymorphism in Java

polymorphism
polymorphism

Polymorphism in Java

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.
Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their type and the class Object.
It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared, the type of reference variable cannot be changed.
The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object.
A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type.
In simple words, polymorphism is the ability by which, we can create functions or reference variables that behave differently in a different programmatic context.

Polymorphism is one of the major building blocks of object-oriented programming along with inheritance, abstraction, and encapsulation.

Important points

1. Polymorphism is the ability to create a variable, a function, or an object that has more than one form.
2. In java, polymorphism is divided into two parts: method overloading and method overriding.
3. Some may argue that method overloading is not polymorphism. Then what does the term compile-time “polymorphism” means??
4. Another term operator overloading is also there, e.g. “+” operator can be used to add two integers as well as concat two sub-strings. Well, this is the only available support for operator overloading in java, and you can not have your custom-defined operator overloading in java.

What is polymorphism in programming?

Polymorphism is the capability of a method to do different things based on the object that it is acting upon. In other words, polymorphism allows you to define one interface and have multiple implementations. As we have seen in the above example that we have defined the method sound() and have the multiple implementations of it in the different-2 subclasses.
 Which sound() method will be called is determined at runtime so the example we gave above is a runtime polymorphism example.
Concept of Polymorphism
Concept of Polymorphism
Types of polymorphism and method overloading & overriding are covered in the separate tutorials. You can refer them here:
 1. Method Overloading in Java – This is an example of compile-time (or static polymorphism)
 2. Method Overriding in Java – This is an example of runtime time (or dynamic polymorphism)
 3. Types of Polymorphism – Runtime and compile-time – This is our next tutorial where we have covered the types of polymorphism in detail. I would recommend you to go through method overloading and overriding before going through this topic.
Examples of Polymorphism
Example of Polymorphism






Let's write down the complete code of it:

 

 

Example 1: Polymorphism in Java

Runtime Polymorphism example:

Animal.java
public class Animal{
   public void sound(){
      System.out.println("Animal is making a sound");   
   }
}
Horse.java
class Horse extends Animal{
    @Override
    public void sound(){
        System.out.println("Neigh");
    }
    public static void main(String args[]){
            Animal obj = new Horse();
            obj.sound();
    }
}
Output:
Neigh
Cat.java
public class Cat extends Animal{
    @Override
    public void sound(){
        System.out.println("Meow");
    }
    public static void main(String args[]){
            Animal obj = new Cat();
            obj.sound();
    }
}
Output:
Meow

 

Example 2: Compile time Polymorphism

Method Overloading on the other hand is a compile time polymorphism example.
class Overload
{
    void demo (int a)
    {
       System.out.println ("a: " + a);
    }
    void demo (int a, int b)
    {
       System.out.println ("a and b: " + a + "," + b);
    }
    double demo(double a) {
       System.out.println("double a: " + a);
       return a*a;
    }
}
class MethodOverloading
{
    public static void main (String args [])
    {
        Overload Obj = new Overload();
        double result;
        Obj .demo(10);
        Obj .demo(10, 20);
        result = Obj .demo(5.5);
        System.out.println("O/P : " + result);
    }
}
Here the method demo() is overloaded 3 times: first method has 1 int parameter, second method has 2 int parameters and third one is having double parameter. Which method is to be called is determined by the arguments we pass while calling methods. This happens at runtime compile time so this type of polymorphism is known as compile time polymorphism.Output:
a: 10
a and b: 10,20
double a: 5.5
O/P : 30.25



Now that you have understood basics of Java, check out the Java Internship by  Surya Informatics , a trusted Internship company with a network of more than 250,000 satisfied Java Developers spread across the globe. 

FOLLOW US ON : YoutubeTwitterFacebookLinkedIn 
 




Thursday, September 5, 2019

Object-Oriented Programming (OOPs)


Object-Oriented Programming (OOPs)



Object-oriented programming: 

As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.
Object-oriented programming
OOPS concept



 1) Class

 The class is a group of similar entities. It is only a logical component and not a physical entity. For example, if you had a class called “Expensive Cars” it could have objects like Mercedes, BMW, Toyota, etc. Its properties(data) can be price or speed of these cars. While the methods may be performed with these cars are driving, reverse, braking, etc.

2) Object

An object can be defined as an instance of a class, and there can be multiple instances of a class in a program. An object contains both the data and the function, which operates on the data. For example - chair, bike, marker, pen, table, car, etc.

3) Inheritance

Inheritance is an OOPS concept in which one object acquires the properties and behaviors of the parent object. It’s creating a parent-child relationship between two classes. It offers a robust and natural mechanism for organizing and structure of any software.

4) Polymorphism

Polymorphism refers to the ability of a variable, object or function to take on multiple forms. For example, in English, the verb run has a different meaning if you use it with a laptopa foot race, and a business. Here, we understand the meaning of run based on the other words used along with it. The same also applied to Polymorphism.

5) Abstraction

An abstraction is an act of representing essential features without including background details. It is a technique of creating a new data type that is suited for a specific application. For example, while driving a car, you do not have to be concerned with its internal working. Here you just need to concern about parts like steering wheel, Gears, accelerator, etc.

6) Encapsulation

Encapsulation is an OOP technique of wrapping the data and code. In this OOPS concept, the variables of a class are always hidden from other classes. It can only be accessed using the methods of their current class. For example - in school, a student cannot exist without a class.

7) Association

Association is a relationship between two objects. It defines the diversity between objects. In this OOP concept, all objects have their separate lifecycles, and there is no owner. For example, many students can associate with one teacher while one student can also associate with multiple teachers.

8) Aggregation

In this technique, all objects have their separate lifecycles. However, there is ownership such that child objects can’t belong to another parent object. For example, consider the class/objects department and teacher. Here, a single teacher can’t belong to multiple departments, but even if we delete the department, the teacher object will never be destroyed.

9) Composition

A composition is a specialized form of Aggregation. It is also called a "death" relationship. Child objects do not have their lifecycle so when parent object deletes all child object will also delete automatically. For that, let’s take an example of houses and rooms. Any house can have several rooms. One room can’t become part of two different houses. So, if you delete the house room will also be deleted.



Advantages of OOPS:

  • OOP offers easy to understand and a clear modular structure for programs.
  • Objects created for Object-Oriented Programs can be reused in other programs. Thus it saves significant development costs.
  • Large programs are difficult to write, but if the development and designing team follow the OOPS concept then they can better design with minimum flaws.
  • It also enhances program modularity because every object exists independently.


Now that you have understood basics of Java, check out the Java Internship by  Surya Informatics , a trusted Internship company with a network of more than 250,000 satisfied Java Developers spread across the globe. 

FOLLOW US ON : YoutubeTwitterFacebookLinkedIn 
 


Thursday, August 8, 2019

Difference between C++ and Java


The fundamental contrast among java and C++ is; Java is stage free language and it is primarily utilized for configuration electronic application yet C++ is stage subordinate language and it is basically utilized for structure work area application.

Difference between C++ and Java
Difference between C++ and Java



Platform In-dependency which gives Portability

C++ is a Platform subordinate language. Along these lines, C++ programs which are incorporated on one machine can just keep running on that specific machine on which they are assembled making it not versatile. 

Where as Java is Platform Independent language. At the point when java projects are ordered they are changed over into some middle of the road code named as byte-code. Presently, which ever machine has JVM application introduced can execute as of now accumulated source document without fretting over working framework at all creation it convenient.     

 Free unused Memory


Java has trash accumulation named string which is in charge of expelling unused articles from the memory and which is naturally produced by JVM. So memory is consequently liberated when not utilized any more. 

C++ has no trash gathering. In this way, all unused held memory should be liberated when never again used to maintain a strategic distance from memory spills. 

Pointers   


C++ bolsters pointers. You can unequivocally announce the pointers which holds the location of another variable and you can pass the location of the variable.

Java doesn't bolster pointers implying that in java you can't unequivocally proclaim the pointers as you can do in C++.

C++ incorporated projects needn't bother with a particular application to execute C++ programs. Where as Java arranged projects needs JVM application to be introduced to execute java programs. 

 Security


Since C++ program permits utilizing pointers, it permits to deal with equipment utilizing pointers and furthermore it needn't bother with any application to execute. In this way, we can create infections utilizing C++ which is perilous for PC. 

Java doesn't bolster pointers and java projects needs JVM application to be executed. Along these lines, Java language forestalls this kind of perilous code.


Multiple Inheritance


C++ bolsters different legacy since it has scope goals administrator(:) utilizing which we can defeat "the uncertainty" occurring during various legacy. 

Java doesn't bolster numerous legacy in light of the fact that a subclass constructor can make call to just a single super class constructor and we can have just a single super(); proclamation in a constructor body which prompts an uncertainty.

 

With Respect to Usage


Java is most likely to be found in Web and Enterprise applications, C++ is more likely to be found in System and other low level applications.


Threading


C++ has no worked in help for strings. C++ depends on non-standard outsider libraries for string support.

Java has worked in help and devoted java.lang.Threads libraries for stringing support.




There are are lot many differences between C++ and Java.


Now that you have understood basics of Java, check out the Java Internship by Surya Informatics Solutions , a trusted Internship company with a network of more than 250,000 satisfied Java Developers spread across the globe.

 

FOLLOW US ON : Youtube, Twitter, FaceBook