Posts

Showing posts from June, 2017

Inheritance in Java

Image
Inheritance Inheritance is one of the key feature of Object Oriented Programming in java. It is a mechanism in which one object acquires all the properties and behaviors of parent object. Inheritance represents the  IS-A relationship , also known as  parent-child  relationship. The derived class inherits the states and behaviors from the base class. The derived class is also known as sub class and base class is also known as super-class. Inheritance is used in Java to achieve Method Overloading (To achieve runtime polymorphism) and Code Reusability. Syntax of Inheritance: class  Subclass-name  extends  Superclass-name   {       //methods and fields    }   Extends keyword represents that you are making a new class which will extend the features of super class. 1.        Superclass-name is super class of Subclass-name. 2.        Subclass-name is sub class of Superclass-name. 3.        Subclass-name IS-A Superclass-name. Disadvantage of Inheritance is

Co-variant Return Type - Part 1

I'm sharing my experience with you as these are the questions which have been asked to me while interviewing on java profile.     What is Covariant return type in java? It means when one overrides a method, the return type of the overriding method is allowed to be same return type of overridden method or subtype of the overridden method's return type. From the release of JDK 1.5, covariant types were introduced in java. Here is a simple example: public class Human {       protected Language speak() {           return new Language ();     } } Dog  class public class Man extends Human {       protected Language speak() {         return new Hindi ();     } } It's possible to modify the return type of the Man's speak() method to Hindi- a sub class of Language, as shown below: protected Hindi speak() {       return new Hindi (); } That's perfectly a legal overriding, and the return type of Man's speak() metho

Polymorphism Part-2 (Method Overriding)

Method Overriding: In object-oriented programming, method overriding is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The benefit of overriding is the ability to define a subclass implementing a parent class method based on its requirement that has same name, same parameters or signature, and same return type as the method in the parent class. An overriding method can also return a subtype of the type returned by the overridden method. This subtype is called a covariant return type. When overriding a method, you might want to use the  @Override  annotation that instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, then it will generate an error. For more information on  @Override , see   Annotations . Rules for Java Metho

Polymorphism Part-1 (Method Overloading)

Polymorphism " Poly " means "many" and " morph " means "form" One task can be performed in different ways. For example: Creating something in different ways like in Automobile industry companies make different models of cars they can use this feature of polymorphism to achieve this functionality. In java, we use method overloading (Compile time, Early binding, Static binding) and method overriding (Run time, Late binding, Dynamic binding). In java, we use method overloading and method overriding to achieve polymorphism. Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc. Method Overloading: It is a feature of creating two or more methods with having same name but different arguments for example if you need to do addition but number of arguments may vary then you should use method overloading in which you will create multiple methods with same method name but different argument types + dif