• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

BJO - 3 main feature of OOP

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jacquie,

There are 3 basic features of object oriented programming language:

Inheritance
Polymorphism
Encapsulation

Could you please explain polymorphism with code. What will be the proper answer, if it is asked in interview.

Thanks a lot.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
inheritance -- taking some characteristics of a superclass , and then may be add some features of your own to create a subclass whicch is much more specific then the subclass

polymorphism -- making a thing behave differently under different circumstances

eg : a superclass object could be used to invoke a overriding method of the sub-class
and the same superclass object could be used to invoke the overridden method in the superclass

encapsulation -- the formal defn means , code and data put together is encpsulation ,so some if class A inherits class B .Then class A cannot modify the datat that is present in class B , it can only use the methods of class B to access the datat of class A
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks bhavesh.

encapsulation -- the formal defn means , code and data put together is encpsulation ,so some if class A inherits class B .Then class A cannot modify the datat that is present in class B , it can only use the methods of class B to access the datat of class A



The proper definition will be " Make attribute private and provide public accessor (getter and setter) method to access them is encapsulation "
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


eg : a superclass object could be used to invoke a overriding method of the sub-class and the same superclass object could be used to invoke the overridden method in the superclass



Something is wrong. You can't call sub class method from super class object.

It should be like this:

A sub class object may call super class method if it is not overridden by sub class.

Hope I am right.
 
bhavesh bhanushali
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Car // this is the super class
{
String color ; // color of the car
String model ; // model of the car
String name ; // name of the car

Car ( String col , String mod , String nm ) // constructor of class car
{
color = col ;
model = mod ;
name = nm ;
}

public void display ( ) // (1)
{
System.out.println ( " The color of the car is " +color ) ;
System.out.println ( " The model of the car is " +model ) ;
System.out.println ( " The name of the car is " +name ) ;

}

// here encapsulation would be methods and data bound together in a single class
}

class Mercedes extends Car // subclass inheritting the class Car
{
// the feilds color , model and name are inheritted from the Car class
// so we need not redefine them again

int capacity ;
int noOfDoors ;

Mercedes ( String col , String mod , String nm , int cap , int no )
{
super ( col , mod , nm) ; // calling the superclass constructor to initialize the
// super class variables
capacity = cap ;
noOfDoors = no ;
}

public void display ( ) // (2)
{ // an example of overriding
super.display ( ) ; // calling the inheritted method
System.out.println ( " The capacity of the car is " +capacity ) ;
System.out.println ( " The number of doors the car has are " +noOfDoors ) ;
}
}

public class MainClass
{
public static void main ( String args[] )
{
Car c = new Car ( "White" , "123" , "boxer" ) ; // creatiing a object of class Car
Mercedes m = new Mercedes ( "Black" , "456" , "SLK" , 300 , 4 ) ; // creating a object of class Mercerdes

// displaying the contents

c.display ( ) ; // calling the display method at 1
System.out.println ( ) ;
m.display ( ) ; // calling the overriding method at 2
System.out.println ( ) ;

c = m ; // the super class object "c" will now point to the subclass obvject
c.display ( ) ; // will call the method at 2
// an example of polymorphism where the super claa object is made to behave like a sub-class object
System.out.println ( ) ;
}
}
 
bhavesh bhanushali
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes u can call a sub class method with a super class object ( that is what is runtime polymorphism ), in the example above i ahve given a example of the same

please check it out and reply back for some clarifications

regards,
Bhavesh
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bhavesh bhanushali:
yes u can call a sub class method with a super class object



You can call a subclass method on a superclass reference variable, IF the method in the subclass overrides a method in superclass.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



yes u can call a sub class method with a super class object ( that is what is runtime polymorphism ), in the example above i ahve given a example of the same



Nope, you can't.

The example you have given is also right. But you might confuse between reference and object.

c.display () ;

Here you are not calling sub class method with super class object. The object is actually belongs to sub class.

Hope it is clear to you now.
 
bhavesh bhanushali
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes you are correct , i didint want to go into the details of the example , so i didint make a distinction between a object and a reference , my bad , next time i will be carefull

regards ,
bhavesh
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rathi ji:
The proper definition will be " Make attribute private and provide public accessor (getter and setter) method to access them is encapsulation "



Accessors still break encapsulation - not as much as public fields, but they still do. Objects should expose behaviour, not data.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Accessors still break encapsulation - not as much as public fields, but they still do. Objects should expose behaviour, not data.



But if you will not provide 'setter method' then how will you *fill* the object???

(If we are providing constructor for this, then it is as good as providing 'setter method')
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rathi ji:

(If we are providing constructor for this, then it is as good as providing 'setter method')



No, it's not "as good", because the constructor only allows you to initialize ("fill") the object, setters also allow you to manipulate the state later.

Additionally, a constructor shouldn't allow you to construct an object that is in an invalid state, i.e. not yet "filled" properly.
 
author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rathi ji:
Hi Jacquie,

There are 3 basic features of object oriented programming language:

Inheritance
Polymorphism
Encapsulation

Could you please explain polymorphism with code. What will be the proper answer, if it is asked in interview.

Thanks a lot.




The term polymorphism refers to the ability of two or more objects belonging to different classes to respond to exactly the same message (method call) in different class-specific ways.

This comes about from a combination of inheritance plus overriding: consider the following three classes:



If we create a collection of assorted types of Student, and iterate through it:



then the line that I've bolded above would be said to be polymorphic, because at run time, the actual type of s -- GraduateStudent vs. UndergraduateStudent -- would cause a different behavior to be manifested by that line of code.

[ EFH: Fixed formatting ]
[ July 13, 2005: Message edited by: Ernest Friedman-Hill ]
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Jacquie and Ilja.

Yes, constuctor is not same as 'setter method'. I missed this point.
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic