Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, 2 May 2017

Abstract Classes Vs Interfaces

It is a bit confusion about when to use an abstract class and when to use an interface.So lets see the differences between abstract classes and interfaces,so that we can use them appropriately!!

Real World Scenario


Let us consider a class wholesaler which represents a wholesale shop with textbooks and stationary like pens, pencils,papers and notebooks

class Wholesaler
{
 void textbooks()
 {}
void stationary();
 {}
}

Saturday, 22 April 2017

Interfaces

Let us discuss one of the interesting concepts of java,interfaces.Interfaces are the extension to the abstract classes.Interfaces are the block of statements that contain only abstract methods.

How interfaces are used ?


If you want to use a block of statements under interface,we use the keyword 'interface' before its name.
interface name
{ 
 methods without implementation
}

How to access methods in interfaces:


methods declared under interfaces can be accessed in two ways.

Creating object to the class:


The most general method of accessing the interfaces are by creating an object for the class that is implementing the interfaces.

Wednesday, 19 April 2017

Abstract Classes

In general abstraction is nothing but hiding of irrelevant information.We Implement abstraction in oop using abstract classes and interfaces.


What is an abstract class


Abstract class implies that,it has one or more abstract method.An abstract method doesn't contain its implementation.


Why do we use Abstract classes:


Let us consider the following scenario.
We have a class named Automobile.It has a member function no_of_wheels.(every automobile has wheels). there are certain classes like Bike,Auto,Car,Bus etc.,.,which comes under automobile class. Here,all vehicles comes under automobile class,but they do not have all the properties same.For example number of wheels is different from each other.

Example:

import java.io.*;
abstract class Automobile
{
 abstract int no_of_wheels();
}
class Car extends Automobile
{
  int  no_of_wheels()
 {
  return 4;
 }
}
class Auto extends Automobile
{
 int no_of_wheels()
 {
  return 3;
 }
}
class Bike extends Automobile
{
 int no_of_wheels()
 {
  return 2;
 }
}
class MainClass
{
 public static void main(String args[])
 {
  Bike b=new Bike();
  Car c=new Car();
  Auto a=new Auto();
  b.no_of_wheels();
  c.no_of_wheels();
  a.no_of_wheels();
 }
}

Can we instantiate an abstract class ?


No,we cannot instantiate an abstract class i.e.,we cannot create an object.This is because,the keyword abstract itself says that it is incomplete.It is just a skeleton.One more thing is abstract methods are not implemented, So what should happen if you create an object for abstract class and try to access the method that doesn't have body? it's not going to work and may cause problem.So if abstract comes before a class name,then JDK tells to JVM to discard that class object initiation.

How to access Abstract Methods:


There are two ways of accessing abstract methods as follows.

1) Creating a reference for the base class.


import java.io.*;
abstract class Code
{ 
 void implement()
 {
 }

}
class Titans extends Code
{
 void implement()
 { 
  System.out.println("Inside Titans class");
 }
}
class MainClass
{
 public static void main(String args[])
 {
  Code c;//creating reference for the abstract class
  c=new Titans();
  c.implement();
 }
}

Output:

Inside Titans class
2)Creating an object for the child classes.


import java.io.*;
abstract class Code
{
  void implement()
  {
  }
}
class Titans extends Code
{
 void implement()
 {
  System.out.println("Inside Titans class");
 }
}
class Mainclass
{
 public static void main(String args[])
 {
  Titans t=new Titans(); //creating object for the derived class
  t.implement();
 }
}

Output:


Inside Titans class

Sunday, 2 April 2017

'this' keyword

lets discuss this keyword,this is a reference to the current class object.this can be used with respect to variables,methods and classes.

this with respect to variables:

this keyword is used to preserve the values of the instance variables.

What is an instance variable ?

A variable that is declared in the class is known as instance variable.Every object of that class will contain a copy of the variable.

Example:

class Code
{
 int var=10;//instance variable
 methodA()
 { 
  ----
  ---
 }
}

Friday, 31 March 2017

Polymorphism-2

In our previous post  previous post,we discussed about static polymorphism i.e,method overloading,Now lets see about Dynamic polymorphism.

What is Dynamic Polymorphism?

Dynamic polymorphism means exhibiting polymorphism at run-time.i.e, the method call is bound to the method body at run time,java compiler do not aware of which method is executed,however JVM knows which method is to be invoked at the run time.Method overriding is an example for dynamic polymorphism.  Method overriding allows us to have two or more methods with same name and same parameter list that resides in two different classes that are inherited.

Example:


class Code
{ 
 void methodA(int a,int b)
 { 
  System.out.println(a+b);
  System.out.println("This computation is performed in Code class");
  }
}
class Titans extends Code
{ 
  void methodA(int a,int b)
  { 
   System.out.println(a+b);
  System.out.println("This computation is performed in Titans  class");
  }
} 
class MainClass
{ 
 public static void main(String args[])
 { 
  //accessing by creating object for derived class
  Titans t=new Titans();
  t.methodA(2,3);
  //accessing by creating object for the base class
  Code c=new Code();
  c.methodA(5,7);
 }
}

Output:

5
This computation is performed in Titans  class
12
This computation is performed in Code class

Saturday, 25 March 2017

Polymorphism-1

In General polymorphism stands for "Existing in more than one form".
In java, polymorphism is divided into two types.
1.compile time polymorphism
2.Run time polymorphism

Compile time polymorphism:

It is also called as static polymorphism.Here binding of methods to its definition takes place at compile time.We implement compile time polymorphism with method overloading concept.

What is method overloading?

Method overloading is a feature that enables us to have a class with two or more methods having same name and differ in parameters resides in the same class.

Example

import java.io.*; 
 class Titans
{
 void method_a(int a,int b)
 {
  int c;
  c=a+b;
  System.out.println("The method with two arguments is exected and result is"+c); 
 }
 void method_a(int a,int b,int c)
 {
  int d;
  d=a+b+c;
  System.out.println("The method with three arguments is executed and result is"+d);
 }
 public static void main(String []args)
 {
  Titans t=new Titans();
  t.method_a(2,3);
  t.method_a(2,3,4);
 }
}

Output :

The method with two arguments is executed and result is 5
The method with three arguments is executed and result is 9

Monday, 20 March 2017

Its 'final' Keyword

Lets discuss one of the important keyword final that appears in java.final can be used with respect to variables,methods and class.

final with respect to variables:

A variable that is declared s a final acts like a constant variable and do not entertain any further modifications.

Example

import java.io.*;
class Code
{
 final int var=10;
 void methodA()
 {   
 var=15;
 System.out.println(var);
 } 

 public static void main(String args[])
 { 
  Code c=new Code();
  c.methodA();
 }
}
Output:
Code.java:6: error: cannot assign a value to final variable var
 var=15;
 

Initialization of final variables:

A final variable that is not initialized at the time of declaration is called a blank final variable. We can only initialize such variables inside the constructor of the class.

Example:

import java.io.*;
class Titans
{
 final int var;
 Titans()
 { 
  var=10;
 }
  void methodA()
 {
  System.out.println("value of var is "+var);
 }
}
class Code
{
 public static void main(String args[])
 {
  Titans t=new Titans();
  t.methodA();  
 }
}

Output:

value of var is 10
A final variable that is declared as a static final and didn't get initialized at the time of the declaration,then it must be initialized only in the static block.

Example:

import java.io.*;
class Code
{
 static final int var;
 static{
 var=20;}
 public static void main(String args[])
 {
  Code c=new Code();
  System.out.println("value of var is "+c.var);  
 }
}

Output:

value of var is 20

final with respect to methods:

A method prefixed with final cannot be overridden.

Example:

import java.io.*;
class Code
{
 final void methodA()
 {
  System.out.println("This methodA() is in class Code");
 }
}
class Titans extends Code
{ 
 void methodA()
 { 
  System.out.println("This methodA() is in class Titans");
 }
 public static void main(String args[])
 { 
  Titans t=new Titans();
  t.methodA();
 }
}

Output:

Titans.java:11: error: methodA() in Titans cannot override methodA() in Code
 void methodA()

final with respect to class:

Whenever a class is declared as final it cannot be inherited.

Example:

 import java.io.*;
final class Code
 {
  int var=10;
 } 
 class Titans extends Code
 { 
 public static void main(String args[])
 { 
  Titans t=new Titans();
  System.out.println(t.var);
 }
}

Output:

Titans.java:6: error: cannot inherit from final Code
 class Titans extends Code
 

Saturday, 18 March 2017

'super' Keyword

Lets discuss about one of the interesting and widely used keyword 'super'.super keyword is used to access the properties of the parent class.

super with respect to variables:

We use super keyword when we want to access the variables of the immediate parent class.

Example:

class Parent
{
 int a=5;
}
class Child extends Parent
{
 int a=10;
 void printvalue()
 {
  System.out.println("Child class variable is "+a);  //Child class member
  System.out.println("Parent class variable is "+super.a);  //Parent class member
 }
}
class Mainclass 
{
 public static void main(String []args)
 { 
  Child c=new Child();
  c.printvalue();
 }
}
Output:
Child class variable is 10
Parent class variable is 5
Here if we do not use super the value of a is '10'(child class) if we use super the value initialized in the parent class is assigned to a.

Sunday, 29 January 2017

RELATING OOP CONCEPTS TO REAL WORLD



Today we have many Object Oriented Programming Languages that use Object Oriented Programming concepts for their existence .Now we are going to see how these Object Oriented Programming concepts can be related to our real world.

There are five important concepts to be discussed here,
       1)Class
       2)Object
       3)Inheritance
       4)Data Abstraction
       5) Data Encapsulation
       6)Polymorphism
 Lets look at one by one..

CLASS and OBJECT:

The primary question that arises is What is class? How to Identify it ? The same questions are applicable even in case of object.Here,you can get a Rule of Thumb.
We can talk about class and object together because one becomes useless without the other.
Class is technically called as logical entity or model for creating objects.
Similarly, Object is known as real world entity or an instance of class.

Logical entity vs Real world entity 

Let us consider the following simple case.
I have my pet dog.Its  name is  Blacky. Now,what becomes my class and object??To find an answer for this,we need to answer two questions.Does really dog exist?The simple answer for this question is No.Because dog doesn't exist in the real world.I cannot see a dog (Even you too..).What about blacky ??It is an object !!! because it exists in the real world,and has all the characteristics of dog.
For example,If Male is a class then Mr.John is an object.(Male doesn't exist in the world,john exist ).