Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help-SCJP-65

 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !

Please anyone guide me regarding the exam.

There are two books---Kathy Seirra
Kathy & berty

Which book to refer for certification ?

Thank You.
 
Marco Piva
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/ScjpFaq#books
 
Bilal A. Siddiqui
Ranch Hand
Posts: 53
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use kathy sierra & bert bates
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

I am not able to get this code: Chapter 2 K&B exercise problem 13
class Mammal
{
String name = "Hello";

String make()
{
return "A";
}
}


class zef extends Mammal
{
String name = "HHH";

String make()
{
return "S";
}
}

public class Zoo
{
public static void main(String args[])
{
new Zoo().go();
}

void go()
{
Mammal m = new zef();

System.out.println(m.name+m.make());
}
}


Output: HelloS

I know m is an object of class zef referencing to class Mammal.

So m.name will display the instance variable of class Mammal.

But what about the method make ?
Why the instance method looks the subclass.

Can anyone explain this ?

Thank you.
Dimple.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because the reference variable m is referring to subclass object so overridden method will be called.
 
Rajiv Chopra
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neha Daga wrote:because the reference variable m is referring to subclass object so overridden method will be called.



This is called Polymorphism.
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neha Daga wrote:because the reference variable m is referring to subclass object so overridden method will be called.


that means when an reference variable is referring to any other class it will invoked the method of the class it belongs.

In other words in this case

Mammal m = new zef();

if make() is a method in both class Mammal and zef

When i say m.make the make method of subclass will be called.

It also means that m.name calls the variable of reference class Mammal

and m.make() calls the method to which it belongs no matter to whom the object m is referring to.
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Chopra wrote:
Neha Daga wrote:because the reference variable m is referring to subclass object so overridden method will be called.



This is called Polymorphism.


i know this is mentioned in the book that

Polymorphism is only with the instance methods.

Topics are not cleared unless practically some code is wriiten and seen how it works.

Thank you.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so dear you have the solution....code code code....
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raju Champaklal wrote:so dear you have the solution....code code code....



I am done with the chapters of K&B i.e 1,2,3,4,5,7 except 6,7,8,9 which i kept at hault fot sometime.

I thought let make first few chapter thro with it proper uderstanding of the class.

Thank you for clearing the doubts.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic