Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 OOP questions
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Panphobia




PostPosted: Fri Jan 25, 2013 8:48 pm   Post subject: OOP questions

What is constructor chaining? Also what is Encapsulation, Polymorphism, and Overloading, I am sure I know what they are, I just don't know the terms.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Fri Jan 25, 2013 9:23 pm   Post subject: RE:OOP questions

You might try Wikipedia.
evildaddy911




PostPosted: Sat Jan 26, 2013 10:13 am   Post subject: RE:OOP questions

POLYMORPHISM: Extending a class and modifying the original class's methods to more suit the new method
example: make an "items" class for a RPG, then extend it and make a "sword" class.


OVERLOADING: have multiple methods with the same name but different parameters. the compiler will use the parameters you used to determine which method you meant and use it.
example:
public double square (double num){}
&
public int square (int num){}

not sure about the other two
Insectoid




PostPosted: Sat Jan 26, 2013 1:19 pm   Post subject: RE:OOP questions

Well, there isn't a wiki on constructor chaining, but it's pretty simple. Say you have a class A. Class B extends class A. The first thing you have to do in class B's constructor is call class A's constructor.
Panphobia




PostPosted: Sat Jan 26, 2013 2:03 pm   Post subject: RE:OOP questions

ahhh so like using the super functions am I correct? and does this() call the current class's constructor?
Insectoid




PostPosted: Sat Jan 26, 2013 2:12 pm   Post subject: RE:OOP questions

No. Calling this() inside a function returns a reference to itself.
TerranceN




PostPosted: Sat Jan 26, 2013 2:29 pm   Post subject: RE:OOP questions

Actually in Java calling this() DOES call other constructors, as seen here.
md




PostPosted: Sat Jan 26, 2013 10:40 pm   Post subject: Re: RE:OOP questions

TerranceN @ 2013-01-26, 2:29 pm wrote:
Actually in Java calling this() DOES call other constructors, as seen here.


It can be used to call other constructors within the same class (as per your example). To call a superclass' constructor you need to use super()
Sponsor
Sponsor
Sponsor
sponsor
Panphobia




PostPosted: Sun Jan 27, 2013 8:53 pm   Post subject: RE:OOP questions

can you use this() to chain constructors in the current class?
Panphobia




PostPosted: Sun Jan 27, 2013 11:39 pm   Post subject: RE:OOP questions

Also what is an advantage of constructor chaining? Why do we use it anyway?
Dreadnought




PostPosted: Mon Jan 28, 2013 12:01 am   Post subject: Re: OOP questions

Not sure if this applies to Java, but in C++ you can't access elements of a parent class in the child class' constructor. So the child class' constructor will have to call the parent class' constructor in the initialization list.
DemonWasp




PostPosted: Mon Jan 28, 2013 12:28 am   Post subject: RE:OOP questions

To clarify:

In Java, you use this(args) to call a second constructor from a first constructor. You can do this repeatedly, but each call to this() must be the first statement in the constructor. This strategy is commonly used for default parameters:

Java:

class Example {
    public Example() { // constructor A
        this ( DEFAULT_NAME ); // calls B, below
    }
    public Example ( String name ) { // constructor B
        this ( name, DEFAULT_AGE ); // calls C, below
    }
    public Example ( String name, int age ) { // constructor C
        this.name = name;
        this.age = age;
    }
}


This kind of method-chaining is pretty common when you want a shorthand for a function that has a lot of parameters. The only thing special happening here is that you say "this()" instead of "ClassName()" and that it must be the first statement in your constructor.

C++11 introduced a similar mechanic, where you can chain constructors within the same class by putting them in the initializer list.
Panphobia




PostPosted: Tue Jan 29, 2013 12:19 pm   Post subject: RE:OOP questions

I just finished my programming exam, it had 70 m/c and 1 programming question, for the programming question it basically told you to create a media abstract class an mp3 subclass and a dvd subclass, and add methods and variables to each, add chained constructors etc, but it also asked you to make an equals method. I did all this according to plan, one problem was to compare the last two elements in the array of media objects and if they are equal you print the contents of the array. I did not cast the object when I compared them, did I need to? what i mean is if I declare a media object as Media m = new DVD(); and I used equals would I need to explicitly cast m.equals()?
Insectoid




PostPosted: Tue Jan 29, 2013 2:05 pm   Post subject: RE:OOP questions

.equals() would have to be defined inside class Media, if you are comparing an MP3 to a DVD. If you want to see if two MP3s are equal, or two DVDs, you should cast to that type (in which case, you should define another .equals inside DVD/MP3).

But I could be wrong, since I didn't pay much attention in this class and haven't used it...ever.
Panphobia




PostPosted: Tue Jan 29, 2013 2:24 pm   Post subject: RE:OOP questions

ahhhh alright, I guess that is the only thing I did not do right then, thanks though, if the last two elements in the array are not the same type then return false, I had the equals method in my mp3 class and dvd class but not one in Media, I needed to cast it right? because it wasnt being overriden?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: