Skip to main content
Known Participant
March 10, 2010
Question

How dose extending classes worke?

  • March 10, 2010
  • 1 reply
  • 503 views

Hello, I dont realy understand how extend workes.. when i extend a class,will it replce that class entery point or will a class that extends another have "two" entery points? And can i pass paramters to the extended class?

Thanks

This topic has been closed for replies.

1 reply

Inspiring
March 10, 2010

In Object-Oriented Programming (OOP) you have to try make your code as modular and re-usable as possible. You start out by making classes called base classes that should contain the most basic and common properties and functionalities. Let's take for instance you have the base class "Creature" that has properties like size, color, intelligence and functionalities (methods) like move, eat, sleep. These properties and methods are the most basic and common of all creatures, don't you agree? Now you can create a more specialized class called derived classes, say class "Human" or class "Dog" by extending (extends) or inheriting from the super/base class so that you don't have to redundantly redefine those common things over and over again when you create other specialized classes. Now you can continue add more specific properties and methods to those new extended classes. You may add speak method for Human and bark method for dog since. These methods can't be put into the common/base class since they are specific. You can even create even more specialized classes by extending again from, say Human, to create classes like "Caucasian" and "Asian". This way you can make projects that are very modular and very easy to maintain. The goal in OOP is to break apart code into as many parts (classes) as possible for easy maintenance.

5millp0xAuthor
Known Participant
March 10, 2010

Thanks man! But what about the entery point? if my class extends "classB" will the classB's entery point fire onCreate?

Inspiring
March 10, 2010

What do you mean by "entry point"? Are you referring to the class' constructor? It will automatically be called when you create an instance of ClassB with the new constructor. Don't forget to call super( base class params ) within the derived class's constructor along with the parameters so that it will first call the base class's constructor then the derived class's constructor.