John
John

Reputation: 335

Basic Java Questions

Hi guys Could you please be kind enough to answer one ore more of these questions? Thanks so much!

Q1. Lets say I initialize an array of "Class A" objects", Can I Also put Class B objects(a subclass of Class A) in the same array. Or do I have to upcast the reference type of Object B to become a class A reference type?

Q2.Can I redefine public methods to be private in a subclass?

Q3.So I understand that if I want to access my superclass' private instance variables I need to use its accessor methods. This is slightly irritating because it doesn't comply with biological inheritance. Why is this the case? Why cant we just treat the private instance variables or private methods of its superclass like it were its own instance variables (what biological inheritance promises)?

Thanks so much! Regards John.

Upvotes: 0

Views: 539

Answers (7)

THINESH VASEE
THINESH VASEE

Reputation: 44

1) yes you can add a subclass to an array of superclass but you don't need to cast 2) No you can't 3)use protected instead of private

Upvotes: 0

Gaurav bhansali
Gaurav bhansali

Reputation: 1

Q1. Yes. A parent class reference variable can hold child class object.

Q2. No. You can not reduce visibility of parent class methods.

Q3. private member are accessible by base class only.

Upvotes: 0

Dhruvam Gupta
Dhruvam Gupta

Reputation: 502

Answer1: You can always put an object of subclass in the reference of super class i.e. A a[] = new A[2]; a[0] = new B(); if B is subclass of class A.

Answer2: no you can't ....

Answer3: in biological inheritance also not everything is inherited to the child...

Upvotes: 1

Petar Minchev
Petar Minchev

Reputation: 47363

Q1. Yes, no need to upcast.
Q2. No
Q3. Use protected instead of private.

Upvotes: 5

Nirmit Shah
Nirmit Shah

Reputation: 758

A1 : Yes. You can add a subclass to a Array of superclass. (e.g, you can add a "Dog" in an array of "Animal"). the basic is all elements in this array should be Animal ( IS relationship)

A2 : No. You can widen the access modifier of method in subclass but cant narrow down. consider this scenario:

Animal a = new Dog();
a.eat();

IF eat method is made private in Dog class, it will result in RunTimeException. (And thats why compiler will not allow you to declare this method as private)

A3 : If you want to use superclass's attribute, make it protected.. Simple! Each modifier has its own importance.

Upvotes: 3

Ralph
Ralph

Reputation: 120761

  • Q1: you do not need to cast explicite
  • Q2: no
  • Q3: because the design goal of private is that this field is private. - That is because of the fact that the programmer has full control of this field, and do not need to pay any attention to any subclass that do something not intended to this field. -- That is the core concept of Object orientation, called Object Hiding / Encapsulation.

Upvotes: 3

Marc
Marc

Reputation: 1976

Q1: You cann also put Class B objects in that array:

private static class A { ...}

private static class B extends A { ...}

A[] arr = new A[] { new A(), new B() }; // works fine

Q2: no

Q3: Use protected instead of private.

Upvotes: 1

Related Questions