Showing posts with label abstract class in java. Show all posts
Showing posts with label abstract class in java. Show all posts

Friday, December 2, 2016

When to use abstract class and interface in Java

In  this post,we will understand and learn when to use abstract class and interface. This is going to the one of the important tricky interview question in any of the core java interview questions. In this page i am going to cover what is abstract and  interface where do we use in real time with examples.Now let us start with definitions and use of them.

abstract class:

An abstract class is a class which contains at least one abstract method that means a abstract method is a method which doesn't have the any implementation it just have the method signature. In order to say a class is abstract then we need to use a keyword  "abstract".

Use of the abstract class:

By using the abstract class we can achieve the abstraction which is one of the OOPS concept. Here abstraction means which shows functionality by hiding the implementation details is called as abstraction.

When to use abstract class:

If you have any requirement where few set of the functionalities common across all the sub classes and few functionalities vary from sub classes then the common functionality you can define in the abstract class. But an abstract class can not instantiated. It can be only be used as a super class for other classes that extend the abstract class.
Abstract classes provide you the flexibility to have certain concrete methods and some other methods that the derived class should be implemented.  So, abstract class is a good choice if you have feature expansion.

If you know partial implementation but not completely then abstract class is good choice.
In addition to abstract method we can declare concrete method also in abstract class. For abstract methods there is no restriction to declare any modifiers like private,protected,final etc..





Example:
abstract class Vehicle
{
public abstract int getNoOfWheels();
}
class Bus extends Vehicle
{
public int getNoOfWheels()
{
System.out.println("no of Bus wheels:"+7);
return 7;
}
}
class Car extends Vehicle
{
public int getNoOfWheels()
{
System.out.println("no of car wheels:"+4);
return 4;
}
}
class Test
{
public static void main(String args[])
{
Bus b=new Bus();
b.getNoOfWheels();
Car c=new Car();
c.getNoOfWheels();
}
}

Output:








interface:
interface is a 100% abstract method. That means it doesn't any implementation. It contains only method declaration. What ever methods declared in interface should be implemented by classes that implement the interface. Remember that a class can implement more than one interface but can extend only one class. Interfaces also can't be create objects like abstract class.


When to use interface:

In java, multiple inheritance problem is solved with a powerful construct called interfaces. interface contains only abstract methods. That is means interface contains only abstract methods there is no concrete methods in interface. interface just specify method declaration but no body of method.

If you use interface you would need to implement all the methods in the class that extends the interface. If you would like to support for future expansion when using interfaces, you will need to extend the interfaces and create new one.

You can use interface if you want a contract on some functionality. You can also use interface to restrict access members of particular type,because all member variables private static and final in interface whether you are declaring or not.

Interfaces are a good choice when you think that the API will not change for a while. Interfaces are also good  when you want to have something similar to multiple inheritance,since you can implement multiple inheritance using interface. 

 Example:

interface one
{
int i=20;
}
class Two implements one
{
public static void main(String args[])
{
int i=30;
System.out.println(i);
}
}

Output:
30

Naming conflict in interfaces:

if two interfaces contains a method with same signature and same return type in the implementation class then only one method implementation is enough.

Example:

interface one
{
void m1();
}
interface two
{
void m1();
}
class Demo implements one, two
{
public void m1()
{
System.out.println("hello java");
}

}

Output:
hello java


Recommended to Read:  Java interface with example


I hope you enjoy this page and comment below with your answers about this topic and share this post through social websites.

Wednesday, October 26, 2016

Java Abstract with Example

We already know that a class is a model for creating the objects. A class contains variables and methods of its objects. There is a rule that any thing written in the class,then that object applicable to all of its objects.For Example,write a method calculate() in the Demo class. If we create two objects to this class,then two objects get the copy of this method,hence from any object we can call and use this method.


Example:

class Demo

{
//method to calculate square value

void calculate(double x)
{
System.out.println("square="+(x*x));
}
}

class Test
{
public static void main(String args[])

{
//create two objects
Demo d1=new Demo();
Demo d2=new Demo();
//call calculate method from the objects
d1.calculate(4);
d2.calculate(5);
}
}
Out Put:

Square=16
square=25

In the above program the requirements of all the objects is same that means calculate square value. But sometimes the requirement of the object will be different.For example the first object want to calculate square value,second object wants the cube value. Then how to write the calculate() method in Demo class.

For this we have to fallow the steps:
  • First write a calculate() method in Demo class, so every object wants to calculate something.
  • If you implement body for calculate() method then it is commonly available to all the objects,so do not write  body for calculate() method. These type of methods are called abstract method.
  • Now derive a sub class square from Demo class,so that the calculate() method available to the sub class.similarly  we create another subclass with name cube where we write the calculate() method.
  • It is possible to create objects for the sub class.Using these objects the corresponding methods will be called.
Abstract method:

An abstract method does not contain any body. Abstract method should have only declaration but not implementation. Hence,Abstract method declaration should end with semicolon(;). The main purpose of abstract method is when the same method has to perform different tasks depending on the object calling it.

Example:

public abstract void m1();//correct declaration
public abstract void m1(){}//wrong one

If you declare abstract method in the parent class,then child is responsible to provide implementation for that abstract methods.

Abstract class:

An abstract class is a class that contains abstract methods. In abstract class contains both abstract methods(without body) and concrete(with body) methods but interface contains 100% abstract methods. Both abstract class and abstract method should be declared by using keyword 'abstract'.



Example:
Now will take The above example where the abstract class Demo has one abstract method which has got various implementations in sub classes.
abstract class Demo
{
//this is abstract method
abstract void calculate(double x);
}
class One extends Demo
{
//calculate square value
void calculate(double x)
{
System.out.println("square="+(x*x));
}
}
class Two extends Demo
{
//calculate cube value
void calculate(double x)
{
System.out.println("cube="+(x*x*x));
}
}
class Test 
{
public static void main(String args[])
{
//create sub class objects
One o=new One();
Two t=new Two();
//call and use calculate()method
o.calculate(4);
t.calculate(5);
}
}
Output:
square value=16
cube=125

Example 2:

 
abstract class Vehicle
{
public abstract int getNoOfWheels();
}
class Bus extends Vehicle
{
public int getNoOfWheels()
{
System.out.println("no of Bus wheels:"+7);
return 7;
}
}
class Car extends Vehicle
{
public int getNoOfWheels()
{
System.out.println("no of car wheels:"+4);
return 4;
}
}
class Test
{
public static void main(String args[])
{
Bus b=new Bus();
b.getNoOfWheels();
Car c=new Car();
c.getNoOfWheels();
}
}
Out put:
no of Bus wheels: 7
no of car wheels : 4

I hope you people understand this concept clearly and share this in social websites and give comments below about on this topic.

 




High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...