473,486 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

java interfaces

5 New Member
Expand|Select|Wrap|Line Numbers
  1. public interface Pet {
  2. public abstract void beFriendly();
  3. public abstract void play();
  4. }
  5.  
  6. public class Dog extends Canine implements Pet {
  7. public void beFriendly() {...}
  8. public void play() {..}
  9. public void roam() {...}
  10. public void eat() {...}
  11. }
If Canine too have a beFriendly and play methods, what method of beFriendly will be executed.
Jan 3 '14 #1
3 2052
Nepomuk
3,112 Recognized Expert Specialist
If Canine has a beFriendly and play method then you have no problem; Dog needs an implementation of both functions (as it implements Pet) and due to it extending Canine it has an implementation of each. If you implement it as you suggest above it won't make any difference though, as Dog implements both functions and so the implementations in Dog will be used.
But here, look at this example:
Expand|Select|Wrap|Line Numbers
  1. public class Canine {
  2.     public void beFriendly() {
  3.         System.out.println("I'm friendly");
  4.     }
  5.  
  6.     public void play() {
  7.         System.out.println("I'm playing");
  8.     }
  9. }
Expand|Select|Wrap|Line Numbers
  1. public class Dog extends Canine implements Pet {
  2.  
  3.     public static void main(String[] args) {
  4.         Dog dog = new Dog();
  5.         dog.beFriendly();
  6.         dog.play();
  7.         dog.roam();
  8.         dog.eat();
  9.     }
  10.  
  11.     public void roam() {
  12.         System.out.println("I'm roaming");
  13.     }
  14.  
  15.     public void eat() {
  16.         System.out.println("I'm eating");
  17.     }
  18. }
This will print the text:
Expand|Select|Wrap|Line Numbers
  1. I'm friendly
  2. I'm playing
  3. I'm roaming
  4. I'm eating
Also, functions defined in interfaces are automatically abstract, so you don't have to put the abstract modifier into an interface.
Jan 3 '14 #2
Anvesh Tummala
5 New Member
If we add the following code in public class Dog extends Canine implements Pet what will be the result
Expand|Select|Wrap|Line Numbers
  1.   public void beFriendly() {
  2.         System.out.println("I'm friendly dog");
  3.     }
  4.  
  5.     public void play() {
  6.         System.out.println("I'm a dog and I am playing");
  7.     }
Jan 3 '14 #3
Nepomuk
3,112 Recognized Expert Specialist
Have you tried this?
Jan 3 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3094
by: Brian Kazian | last post by:
I want to insure that all subclasses implement a certain method, but could not find anything that would do this for me. Is there anyway in Python to implement this check? Thanks!
13
1835
by: rajkumar | last post by:
Hey all Here is my question Consider two classes Class A : SomeSystemClassA Class B : SomeSystemClassB Now I have to add a common interface to both class A and class B. The
6
1810
by: s99999999s2003 | last post by:
hi i come from a non OO environment. now i am learning about classes. can i ask, in JAva, there are things like interface. eg public interface someinterface { public somemethod (); .... ... }...
2
1132
by: needin4mation | last post by:
I am looking for a good book on C# that has what an interface is and how to use it. In my local library they have a lot of Java books. Is an interface a C# thing or an OOP thing? So that if I...
1
1169
by: vicky kirplani | last post by:
hello dear if anybody know about java interfaces so plz help me my topic is. interfaces packages event handling anyone solve my this topic so plz help me Vicky
1
2680
by: bea | last post by:
Hi. I'm trying to call a stored procedure and it executes a external Java class but I have this error: SQL4304N Java stored procedure or user-defined function "SENDMAIL", specific name...
10
1974
by: hyperboreean | last post by:
Hi, Probably it has been asked before, but I'll still ask. Why doesn't python provide interfaces trough its standard library? Or it was ever proposed to be included in the language? Zope's...
1
5943
by: okonita | last post by:
Hello all, I have a Java problem that I hope can be answered here. Very new to DB2 UDB and UDF (we are on DB2v9.5, Linux and Windows), I have managed to get the UDF registered but having problem...
28
1630
by: jmDesktop | last post by:
Studying OOP and noticed that Python does not have Interfaces. Is that correct? Is my schooling for nought on these OOP concepts if I use Python. Am I losing something if I don't use the...
63
4891
by: s0suk3 | last post by:
I've been programming Python for a couple of years now. Now I'm looking to move on to either C++ or Java, but I'm not sure which. Which one do you think will be a better transition for a Python...
0
7094
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4863
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.