• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface in java  RSS feed

 
Vikash Pathak
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interface vs Abstraction in Java
 
Vikash Pathak
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome everyone to discuss Interface and Abstraction in JAVA..
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please tell us what you think about interfaces versus abstraction.
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract Class:

a) An abstract class is a special kind of class that cannot be instantiated.
b) It allows the other classes to inherit from it but cannot be instantiated.
c) It is used to carry the same hierarchy or standards for all the subclasses.


Interface:

a) An interface is not a class,but it is an entity and has no implementation.
b) It has only the definition of the methods without the body.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're asking about the difference between an interface and an abstract class, then see this JavaRanch FAQ page: Interface vs. Abstract Class
 
Vikash Pathak
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats nicely explained bt it would have been really good if the discussion will be going on with some good examples... :-)
 
jack java
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider a scenario where all Cars will have 4 tyres and other features can be different.In this case any subclass of Car has to have 4 tyres. This is a case where abstract class will be used and a default implementaion for tyres will be provided.

public abstract class Car{
public abstract String getCarName();
public final int getNoOfTyres(){
return 4;
}
}

Consider a scenario where Cars can have any number of tyres and other features can also be different. In this case interface will be created.

public interface Car{
public abstract String getCarName();
public abstract int getNoOfTyres();
}

 
Vikash Pathak
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JackJava
But that is not clear why we are using the interface instead of the abstract class and what benefit we are going to reap off it! It will be better if explicitly explained..
 
Vikash Pathak
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@All:

interface Pet{
public void test();
}
class Dog implements Pet{
public void test(){
System.out.println("Interface Method Implemented");
}
public static void main(String args[]){
Pet p = new Dog();
p.test();
}
}

If we take this code into consideration, what really does happens when the the test() is called inside the interface pet{} ? what happens when any pre-declared method is being called inside the interface?
 
Vikash Pathak
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know much but that is the reason we are here.. lets discuss the coding and use theory where ever needed.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ignore that post; it wasn’t at all helpful.
You cannot understand the code if you don’t understand the theory.

What happens if you call the method inside the interface: well, you can’t call the method in the interface in the first place because the interface hasn’t got any methods. It only has parts of methods. They have headers and no bodies.
What you are doing is calling the implementation in the class. What happens is whatever you wrote in that method.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikash Pathak wrote:If we take this code into consideration, what really does happens when the the test() is called inside the interface pet{} ? what happens when any pre-declared method is being called inside the interface?


It really comes down to what you are really calling a method 'on.' When you do something like this: something.aMethod() you are not calling the method from an interface, or the method from a class, you are calling a method on the object - the instance. When you are define a variable's type you are basically telling the compiler what methods and fields the object will have - this is a check when you compile the code to make sure that you only call methods that actually exist. At runtime, the method (which should already be known to exist because the code was compiled) is located in the specific object and called.

So back to code a little bit:


To summarize, we have an interface called Runnable with a run() method, and a class which implements it by counting. We have a tester class which has two methods: one takes an instance of Counter, sets the value to count to and calls its run() method. The other takes an instance of Runnable and calls its run() method. Both methods know that the object passed in as a parameter has a method named run() because the types Runnable and Counter both define that method. But the doTheCounting() method knows the object also has a setCountTo() method, while doTheWork() does not, so doTheCounting() can do counter.setCountTo() but if doTheWork() tried to it would get a compile error.

When you execute the code, the class/interface has less to do with it. The compiler already made sure that the objects that would be passed to the doTheWork() and doTheCounting() methods will be able to do what is required. So when it runs, the doTheWork() method gets the run() method associated with the specific object passed to it, and executes it. This is the same run() method that doTheCounting() uses, because they both get the same object, even though one is referring to it as a Runnable, and the other a Counter.
 
Vikash Pathak
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is not Executing.. First it asks for a pair of the braces as your given specifications. When i make it a single class, it is demanding the name of the class should be of the Interface and when i am making the name of the file as of the interface it is demanding the name to be as of the class name. It is really messy..
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikash Pathak wrote:The code is not Executing.. First it asks for a pair of the braces as your given specifications. When i make it a single class, it is demanding the name of the class should be of the Interface and when i am making the name of the file as of the interface it is demanding the name to be as of the class name. It is really messy..

Sometimes assumptions are made when posting code. Here Steve was assuming you knew that all public classes/interfaces have to be declared in a file with the same name as the class/interface.
In this case it appears the assumption was wrong, so to make things clearer - as all the classes/interfaces in Steve's post are public they need to be in separate files whose names match the class/interface name.
 
Vikash Pathak
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah. Whatever you said was right.
Bt my qs still remains the same. I wanted to know what Really does happens Inside a interface/ what if we just have the Object Of the class and access the method Which Is to be Called inside the interface. Output is same. Then what Role does interface plays?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikash Pathak wrote:Yeah. Whatever you said was right.
Bt my qs


Please UseRealWords.(←click)

still remains the same. I wanted to know what Really does happens Inside a interface/ what if we just have the Object Of the class and access the method Which Is to be Called inside the interface. Output is same. Then what Role does interface plays?


When you want to define WHAT a type can do, or WHAT type you require, without worrying about HOW it does it, that's what an interface is for.

Example 1: Collections and Lists. If I want something where I can add and remove items, and iterate over the items I have added, and I don't care about uniqueness or ordering, I can specify that I want a Collection. I don't care if it's a LinkedList or ArrayList or HashSet or whatever. I just need some bunch of things that I can process one by one.


On the other hand, if I want things to be processed in the order in which they are added, I can specify a List, but in most cases, I don't care if it's a LinkedList or ArrayList.


Example 2: JDBC.


Connection, PreparedStatement, and ResultSet are all interfaces. Each DB vendor has their own way of implementing them, and names their classes however they want. My code doesn't have to know or care what the specific classes are. All I care about is that, for example, the implementation of Connection will have a prepareStatement() method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!