• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Interfaces and abstract classes

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand the interface code below. Specifically, the methods inside the interface. How can you make a method of a class? I'm not really grasping the "public Address" and "public PhoneNumber" part. Can someone please explain why this is done and what is the advantage....

 
lowercase baba
Posts: 13002
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'public' means anybody can call the method.

'Address' and 'PhoneNumber' are the return types of the respective methods. It makes sense that the method "createAddress()" would return an Addresss object, yet?
 
dekij ra
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I'm having difficulty to conceptualize what it would return exactly. I understand if it returns a String or an int or boolean.....but I'm having difficulty conceptualizing the Address object that it would return. Does that mean that it would have values for the address fields (i.e. values for street, city, region, etc)?
 
fred rosenberger
lowercase baba
Posts: 13002
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can see what it would have in the code you posted. The Address class is defined to have four strings: street, city, region, and postalCode.

you can then call the methods you define:

getStreet()
getCity()
etc...

Note that you have the Address class defined as abstract, although I'm not sure why. Normally, an abstract class would have one or more methods declared but not implemented. In your Address class, they all appear to be implemented.

If this is supposed to be an abstract class, then you would need some other concrete (i.e. non-abstract) class that extends it. That sub-class would be BOTH an Address and a (whatever you named the concrete class) in the same way a dog is both a Mammal and a Dog.

You can assign that returned object to any legal reference - and an Address reference certainly works.

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic