• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java IS-A relationship  RSS feed

 
mishra ashu
Greenhorn
Posts: 2
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone tell me java example of IS-A relationship;
I am giving java example of Has-A relationship----> System.out.println
here 'out' is a static reference variable of print stream class and println is a method from print string class.
 
mishra ashu
Greenhorn
Posts: 2
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got the answer "Every java program"
As every java should inherits properties from object class.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither of those are good explanations of the concepts you are associating with them.

Sure, all Java classes are subclasses of the base Object class but that's like saying that people are carbon-based organisms. You need to be more specific to understand the concept more deeply.

Is-a relationships in Java are established through the extends and implements keywords.

So if you write class B extends A, you are saying that B "is a" kind of A.

When you write class D implements C, this means that instances of class D can be assigned to reference variables of type C because any D object "is an" implementation of the C interface.
 
Aqsa Anum
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Neither of those are good explanations of the concepts you are associating with them.

Sure, all Java classes are subclasses of the base Object class but that's like saying that people are carbon-based organisms. You need to be more specific to understand the concept more deeply.

Is-a relationships in Java are established through the extends and implements keywords.

So if you write class B extends A, you are saying that B "is a" kind of A.

When you write class D implements C, this means that instances of class D can be assigned to reference variables of type C because any D object "is an" implementation of the C interface.


Can you give a little coding example on "implements" in java ?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Junilu is right; that is not a good example. There are no properties to inherit from Object, only methods. Which of course you are probably going to change.
The out field is a static field of the System class, so that isn't a good example.
What about Animal→Dog, Shape→Square, List→ArrayList, Vehicle→Morotcycle?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!