Skip to main content

Posts

Showing posts from August, 2014

Super keyword in java

super keyword The  super  is a reference variable that is used to refer immediate parent class object. Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable. Usage of super Keyword super is used to refer immediate parent class instance variable. super() is used to invoke immediate parent class constructor. super is used to invoke immediate parent class method. 1) super is used to refer immediate parent class instance variable. Problem without super keyword class  Vehicle{      int  speed= 50 ;   }      class  Bike  extends  Vehicle{      int  speed= 100 ;             void  display(){      System.out.println(speed); //will print speed of Bike     ...

Abstraction in Java

Java - Abstraction             Abstraction refers to the ability to make a class abstract in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class. If a class is abstract and cannot be instantiated, the class does not have much use unless it is subclass. This is typically how abstract classes come about during the design phase. A parent class contains the common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own. Abstract Class: Use the  abstract  keyword to declare a class abstract. The keyword appears in the class declaration somewhere before the class keyword. /* File name : Employee.java */ public abstract class Employee { private String name; private String address; priva...

Polymorphism 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 define one interface and have multiple implementations. I know it sounds confusing. Don’t worry we will discuss this in detail. It is a  feature that allows one interface to be used for a general class of  actions. An operation may exhibit different behavior in different instances. The behavior depends on the types of data used in the operation. It plays an important role in allowing objects having different internal structures to share the same external interface. Polymorphism is extensively used in implementing inheritance. Following concepts demonstrate different types of polymorphism in java. 1) Method Overloading 2) Method Overriding Method Definition: A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simp...