• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clarification on a private non abstract method in Abstract classes

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this code snippet below,



this line 18 throws a compiler error Abs2 abs = new Child4();, saying that method is not visible.

1.how can we access the Swim() method of the Abstract class through the abs object.
2. What is the use of the private method in an Abstract class if it cannot be accessed.
3.we cannot access the child class's Swim method through the abs object because it is essentially not overridden as its marked private in the super class ->is my understanding right?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:1.how can we access the Swim() method of the Abstract class through the abs object.

First of all, abs is not an object, it's a reference variable which refers to an object (a Child4 instance). Use another access modifier: protected or public. Another option: don't use any access modifier at all (thus simply remove private), the Swim method will have default access and because both classes are in the same package that will work as well.
And another one: move main() method to class Abs2 (but then you'll have a compiler error on this line c4.Swim(); )

Ramya Subraamanian wrote:2. What is the use of the private method in an Abstract class if it cannot be accessed.

For the same reason a concrete class has private members (fields and methods).

Ramya Subraamanian wrote:3.we cannot access the child class's Swim method through the abs object because it is essentially not overridden as its marked private in the super class ->is my understanding right?

No, that's incorrect! If you use the reference variable abs, you can only invoke the methods defined in class Abs2 (because that's the type of the reference variable). You can never invoke methods from a subclass using a reference variable of the superclass. But it could be that by invoking a superclass method, a subclass method is executed. That's called polymorphism. If you change access modifier of both Swim methods to e.g. protected, you'll have a working example of polymorphism

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the same reason a concrete class has private members (fields and methods).


I agree with you.I will just create the Concrete class's reference variable and access the private members. But in the case of an abstract class, is there a way to access the private method(as abstract classes cannot be instantiated)?

You can never invoke methods from a subclass using a reference variable of the superclass.


do you mean a reference variable like abs ? the output of the code is :Child Swim (from the subclass method).

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:I agree with you.I will just create the Concrete class's reference variable and access the private members. But in the case of an abstract class, is there a way to access the private method(as abstract classes cannot be instantiated)?

You can access them from within other methods of the abstract class... With a concrete class you can access private members also only from within the class. That's exactly the same!

Ramya Subraamanian wrote:
You can never invoke methods from a subclass using a reference variable of the superclass.
do you mean a reference variable like abs ?

Yes, because reference variable abs is of type Abs2 which is the superclass.

And once again, in the code snippet you didn't invoke the Swim() method of subclass Child4, you invoked the Swim() method of Abs2. Because like I stated before: you can never invoke methods from a subclass using a reference variable of the superclass. Illustrated in this code snippet

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic