🔒 Closed Polymorphism vs inheritance

Status
Not open for further replies.

sobciana

Enthusiast
Good day po. Ask ko lang po kung sino pwede magpaliwanag ng madali kung anong pinagkaiba nila medyo nalilito ako. Yung sariling explanation nyo po sana para mabilis maintindihan
 
Example of polymorphism

public int add(int a) {
return a + 1;
}
public int add(int a, int b) {
return a + b;
}

Notice to 2 functions of the same name but have different parameters thats polymorphism

Ang inheritnace naman is magagamit mo ang functions from other classes as long as the "extends" keyword is invoked to that class.

Class A extends B

In which all functions, variables that are declared on Class B is inherited by Class A as long as not declared private,

Example:
Class B has declared a global variable of int number,

Extends is invoked to A,

So Class A can use the global variable int number and use it,but can be overriden by using Override.
 
Salamat po codyscott medyo naliwanagan ako kahit paano lalo na po doon sa page 3 kung paano talaga sya naiapply sa pagcocode mo.
 
Salamat po codyscott medyo naliwanagan ako kahit paano lalo na po doon sa page 3 kung paano talaga sya naiapply sa pagcocode mo.
You are welcome.

Just keep on coding and practicing without paying attention too much on the MEANINGS ng mga terms yan (kung ayaw pumasok sa utak sa first time).

I am sure next week, or next month, malilimutan mo na uli. But whenever you encounter it again, go back and read about it para ma-refresh uli sa utak mo.

Repetition is the key (kahit ako, I had to read the meaning again to refresh my mind before I explain it here.)

;)
 
You are welcome.

Just keep on coding and practicing without paying attention too much on the MEANINGS ng mga terms yan (kung ayaw pumasok sa utak sa first time).

I am sure next week, or next month, malilimutan mo na uli. But whenever you encounter it again, go back and read about it para ma-refresh uli sa utak mo.

Repetition is the key (kahit ako, I had to read the meaning again to refresh my mind before I explain it here.)

;)

Last na po. Ask lang po kung para san talaga ginagamit yung super()? medyo nalilito ako kasi minsan nakikita ko sya sa loob ng mga class e
 
"super" ay keyword na ginagamit para lang i-access ang method or variable ng SUPERCLASS.

(Sa mga halimbawa sa PDF, ang SUPERCLASS ay IBON....subclasses niya ang Maya, Chicken at Bibe. Sa Pangalawang illustration, PERSON is the SUPERCLASS....subclasses niya ang Student, Teacher, at Pedro...then sa Pangatlong illustration, JFrame ang SUPPERCLASS...subclass niya ang MyApp)

REMEMBER, pag subclass ka...puwede mo i-inherit ang mga methods at variables ng superclass????
(Like Bibe, puwede niya i-inherit lahat ng methods at variables ng Ibon)

...Dito na papasok ang paggamit ng "super"....dyarannnnnnnnn..
So si subclass puwede i-inherit ang methods...AND.... ang subclass puwede rin niyang i-OVERRIDE ang method na inin-herit niya.

EXAMPLE:

************superclass natin Ibon at meron siyang method na sing()******
Code:
public class Ibon{
       public void sing(){
         System.out.println("Singing from SuperClass Ibon");
      }
}
*****end ng superclass code**********

*******subclass natin, Bibe at i-inherit niya ang sing() method ********
Code:
public class Bibe extends Ibon{
     public void sing(){
             super.sing();
            System.out.println("Singing from subclass quack quak");
      }
}
****end of subclass code******************

So, si Bibe, nag inherit sa sing method ng SuperClass Ibon....
Tapos gusto ni Bibe i-override (or palitan ng konti).

super.sing() = ibig sabihin "gamitin ang sing() method na nasa Superclass)


Hope this clears out a bit.... dahan dahan... the more you practice, the more you will get the idea.

;)
 
Salamat po sa effort ng pageexplain kahit papano naliliwanagan ako kasi hindi ko masyado magets talaga yung mga nasa google ng mas malalim although naiintindihan ko naman. Thanks po mga sir
 
Status
Not open for further replies.

About this Thread

  • 11
    Replies
  • 730
    Views
  • 4
    Participants
Last reply from:
sobciana

Online now

Members online
315
Guests online
960
Total visitors
1,275

Forum statistics

Threads
2,275,089
Posts
28,960,571
Members
1,233,588
Latest member
maikera97
Back
Top