Question Java and OOP principles

JC0133

Senior member
Nov 2, 2010
201
1
76
So what is pass by value and what is pass by reference in Java? By that I know you have to pass primitives by value and objects by reference correct? What else is forced to be passed by value and reference?

The super class calls the constructor but I don’t understand the point of that since all sub-classes have access to the parent class methods anyway?

Jave has an object type can somebody explain to me how that works?

For abstract classes can you have all abstract methods or NO abstract methods?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,360
4,067
75
Ooh, I remember a lot of this. Although I haven't touched Java in years, so newer versions might have changed things.

As I recall, Java is a "pass reference by value" language. When you pass a variable to a method you're always passing by value. What you have to understand is that each variable holds either a primitive or a reference, which is like an immutable pointer to an object. Objects never get passed to a function; only references to them. If you "assign a different object to a variable", what you're really doing is assigning it another reference. So if you modify an object referenced by a variable that was passed in, the object is modified as if it were passed by reference; but if you assign the variable another reference, the original object is unchanged.

If you modify an Object you're not copying it. But remember that when you "modify" an immutable Object, such as a String, you're always making a copy of it.

I think that about covers Objects. Oh, and all Objects are subclasses of Object, whether you like it or not.

The super class calls the constructor but I don’t understand the point of that since all sub-classes have access to the parent class methods anyway?
By this do you mean that a subclass has to call a superclass' constructor, or that superclass constructor doesn't run when you instantiate the subclass? I think that's right.

I'm afraid I didn't get into abstract classes much. The general rule for me was either you have a concrete class or you use interfaces, which have all abstract methods. I think with abstract classes you could have concrete methods alongside abstract ones, but when extending the class you had to implement all the abstract methods.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
Ken is spot on when it comes to by value/reference. Java does neither and both .

The super class calls the constructor but I don’t understand the point of that since all sub-classes have access to the parent class methods anyway?

Abstract classes are allowed to have fields that need to be setup. That is what the constructor is for. Sub-classes need to call their super constructor to ensure everything is setup inside of the super class.

Jave has an object type can somebody explain to me how that works?

All "classes" in java extend Object (for now at least...). The Object class dictates basic functionality that all Objects should have. For example, equals and hashCode.

For abstract classes can you have all abstract methods or NO abstract methods?

Yes, there is no restriction on how many abstract methods an abstract class must have. In fact, you can have no methods at all. Heck, an abstract class can be totally empty even! Abstract classes only enable you to have abstract methods, that's it. You don't have to use them.

By this do you mean that a subclass has to call a superclass' constructor, or that superclass constructor doesn't run when you instantiate the subclass? I think that's right.

A super class constructor always runs first, at least in java (there is no bytecode restriction that prevents you from running it later).
 
Reactions: Ken g6

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,360
4,067
75
A super class constructor always runs first, at least in java (there is no bytecode restriction that prevents you from running it later).
I've been piecing my memories back together. I think it's a case that the default constructor, super(), always runs; but if your constructor has arguments that the parent class' constructor also needs, you need to manually call super(with, those, arguments).
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
I've been piecing my memories back together. I think it's a case that the default constructor, super(), always runs; but if your constructor has arguments that the parent class' constructor also needs, you need to manually call super(with, those, arguments).

Basically. If no constructor is specified, the default is attempted. If the abstract specifies non-default constructors then the implementer must call one of them.
 
Sep 29, 2004
18,656
67
91
Everything is by reference except primitives.

Atelast for development that is true. I have no idea what is going on under the hood in regards to autoboxing.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
As I recall, there are some cases where it makes a difference that Java is "pass reference by value", not "pass by reference". It doesn't happen often, but I'm sure it does happen.

That is how it works.

For example,

Code:
void foo(Integer baz){
  baz = 42;
}

The above will only change baz to 42 locally to method foo. It will not change the caller's baz.

Compare that to pass by reference from C++

Code:
 void foo(int& bar) {
  bar = 42;
}

The above will change the caller's bar into 42. That is not possible with java.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |