• 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

How to inherit methods and variables and override some method of a super class

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have two classes called A and the second one is B. they have 3 methods which are completely the same and also some variables that are completely the same too. but their diffrence is just in on method yhat should be overrided in each class. Now I want to know How can I create a class such a super class in java that has those 3 common methods and common variables but has a method which is like abstract method in java. and then each class get those methods and variables from that class and override the method which makes the A and b class be diffrend with each other!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the Oracle Java Tutorial (which should always be your first stop for general Java questions) on inheritance: http://docs.oracle.com/javase/tutorial/java/IandI/index.html
 
elenora Rezaie
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply. Do you mean that my super class is a kind of abstract class and I should define the uncommon method between A and B classes as abstract as well? and in class A and B I should extend super class?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know. I don't think you have provided enough details for anybody to give a definite answer.
If you have two subclasses Foo and Bar, and a potential superclass, Baz, can you confidently say this?

A Foo is a Baz
A Bar is a Baz

The fewer fields you add in the subclasses and the fewer methods you add, the more likely it is that you can say there is an “IS‑A” relationship. If you can really say there is such an “IS‑A” relationship, and you can see that the methods do not breach the Liskov Substitution Principle (LSP), then make the classes extend each other. You can see an example of a method which doesn't fulfil the LSP here (see page 5), and how the equals method can breach the LSP in Joshua Bloch's Effective Java™chapter 3.
Can you make your classes all implement a common interface? Can you use composition, so you have a Baz as a field?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

elenora Rezaie wrote:Hi, I have two classes called A and the second one is B...


I doubt you do. In the real world, those classes will have names, not letters; and what those names are is very important.

If this is simply a question from a book or course, then you may have only been given A and B, but it's worth thinking about some real-life scanerio that they might exist in. But in order to do that you need to give them names.

For example: from your description, I have no idea whether B is a subclass of A or not. They could easily be siblings, both of which are subclasses of some parent class, or interface.

My suggestion: Try and come up with some names for A and B, and then ask the same question to yourself and see if it still makes sense. if it doesn't, then come back and ask us the same question - but give us those names. We'll soon tell you if they don't make any sense, but we may be better equipped to help you with the rest of your question.

Winston

 
reply
    Bookmark Topic Watch Topic
  • New Topic