• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Employee class - Abastract Vs Interface

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Employee {

public static void main(String args[]) {

String name;
String deptName;
String address;

}

}

If i want to access particular employees department for example employee.getDepartment() which design approach is better ? (Abstract / Interface)
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why the class Employee now it's not good for you?

Usually we use Abstract or Interface when we will have more class with comum subject.

Like, List.

We have interface List and "sub classes" like, LinkedList, ArrayList....

If you wont have any other class such as Employee, why creating one of those?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just so you're aware, the posted code isn't really doing anything--it declares some local variables in a static main method, which is a lot different than an employee class with properties.

That said, the question doesn't really make any sense as it stands: just an interface won't give you what you want--interfaces define a *contract* (in Java's limited way) but provide no implementation. An abstract class comes closer. Which is "better" depends entirely on your needs, and the two are not mutually exclusive. You could have an Employee interface that defines a contract, and an abstract (or concrete) class that implements that interface.
 
Ranch Hand
Posts: 151
MyEclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so for so i know
abstract and interface can't have main method because you can't instantiate them.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

akhter wahab wrote: . . . abstract . . . can't have main method because you can't instantiate them.

I think that is incorrect. Try it.
 
akhter wahab
Ranch Hand
Posts: 151
MyEclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the worng info dear thats why i wrote so for so .....

i tried it


but i really don't understand for what we should use this ??? can you please explain it why we use main method in the abstract class
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably wouldn't.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not a case of whether one would use it, but whether one can use it. Put a main method into that abstract class with a System.out.println("In abstract main method.); call in, and run it and see what happens.
 
akhter wahab
Ranch Hand
Posts: 151
MyEclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
some one said me that i am instantiating this abstract class anonymously ......... is it? and as i know if we are not creating the reference than it is anonymous but i have NewClass Reference as c in this code
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A subclass of NewClass is still a NewClass.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

akhter wahab wrote:some one said me that i am instantiating this abstract class anonymously

I believe I was that "someone".
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it create new Class which extends that abstract class , you can create object of abstract class anonymously ,until you don't have any abstract method in your abstract class..
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, writing {}; provides a class body which implements all the abstract methods you don't have. You can provide anything you like in an anonymous class, except a constructor.
 
akhter wahab
Ranch Hand
Posts: 151
MyEclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ravindra patil wrote:it create new Class which extends that abstract class , you can create object of abstract class anonymously ,until you don't have any abstract method in your abstract class..



hey we can make anonymous object of abstract class even it have abstract method too i tried this

 
akhter wahab
Ranch Hand
Posts: 151
MyEclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

akhter wahab wrote:some one said me that i am instantiating this abstract class anonymously

I believe I was that "someone".


yup you are the someone


Campbell Ritchie wrote:Yes, writing {}; provides a class body which implements all the abstract methods you don't have. You can provide anything you like in an anonymous class, except a constructor.


thanks for such a valuable information for me i got it
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
kri shan
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether abstract class are really needed ? We can control the object creation/instantiation of the class thru private constructor(not only thru abstract class). Interface is kind of abstract base class.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, they're needed, if you want to provide any implementation. Control of object creation doesn't have anything (or at least not very much?) to do with it.
 
kri shan
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if you want to provide any implementation

means providing non abstract method implementation in the abstract base class?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct!
 
reply
    Bookmark Topic Watch Topic
  • New Topic