Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


Shrabanee Mishra
Object slicing
1 Answer(s)      2 years and 7 months ago
Posted in : Servlet Interview Questions


I have a program as bellow:

#include<iostream.h>
#include<conio.h>
class A
{
    public:
    int x;
    A()
    {
        x=10;
    }
};

class B:public A
{
     protected:
     int y;
     public:
     B()
     {
        y=30;
     }
};

void main()
{
    A obj1;
    B obj2;
    obj1=obj2;
    cout<<obj1.x;
    cout<<obj1.y;
    getch();
}

The error message is "y is not member of A"

Can any one solve this problem?


Advertisement
View Answers

August 23, 2012 at 3:55 PM


There is nothing to solve here. The error message displays correct that "y is not member of A".

When you assign a derived class's instance into the base class only that part, which are present in the base class are copied, rest are not copied. this is called Object Sliced. because here obj2 get sliced and stored in obj1.

The main reson for this is, it is not possible (or meaningful) in any statically typed language for a superclass instance to contain subclass member variables.



Related Tutorials/Questions & Answers:
object
object  is it possible to create object in the same class..?.   Yes, you can. class CreateObject { CreateObject(){ System.out.println("I have an object."); } public static void main(String[] args
object of object class
object of object class  what do u mean by "object of object class
Advertisements
Object
Object       An object...). In an object, variables store values for later use and methods are the unit... are the basic units of the object-oriented programming. Objects are the part of our day
object creation
object creation  when will java object is created? is it at runtime or compiletime
Object Oriented
Object Oriented  C++ is Purely object oriented or not .Then why java called purely object oriented
Object reference not set to an instance of an object
Object reference not set to an instance of an object  Object reference not set to an instance of an object
Session Object
Session Object  Why do we require Session Object?  Hello.... The session object is used by the developers to store and retrieve user's related data such as login details. The javax.servlet.http.HttpSession object
ArrayList object
ArrayList object  i have am ArrayList object it containg data base records in it, now i want to display this ArrayList object data in my jsp programe, plz help me
Java object
Java object  Which class should you use to obtain design information about an object
Object Creations
Object Creations  String a[][]=new String[10][20]; The above line how many objects are created
Java object
Java object  What is mutable object and immutable object?  An Immutable object is a kind of object whose state cannot be modified after it is created. This is as opposed to a mutable object, which can be modified
Session Object
Session Object   What is the Max amount of information that can be saved in a Session Object ?   There is no such limit on the amount of information that can be saved in a Session Object. The only limit is the Session
object array
object array  Hi i have array that is object[] obj= { new string("hi"), new vector().add(10), new hashmap().setkey()} display(obj); display(object{[] obj) {} Now my question is what is the string length and how to retrieve
Vector object[]
Vector object[]  I want to create payslip in java public Vector...(); Object[] value = new Object[] {"Employee Name",f.ename.getSelectedItem()}; rec.add(value); value = new Object[] {"Mary-Kate Olsen", "Ashley
class and Object
class and Object  how many classes,References and objects in this following statement.... A a=new B();   Hi, In the statement: A a=new B(); There are 2 classes, Class A and Class B. References : 1 (Object of B
implect object
implect object  how i implment implect object   Hi Friend, Please visit the following links: http://www.roseindia.net/help/java/o/jsp-implicit-objects.shtml http://www.roseindia.net/jsp/jspfundamentals.shtml Thanks
constructor or object
constructor or object  object or construct which create first?   A constructor is always created first. Here is an example: class Example { Example(String st){ System.out.println(st); } public
Class and object
the object of subclass Thanks
Java Object
Java Object         Object is the basic entity of object oriented programming language. Object... properties of the class or its group. Java object is an instance of the class. It takes
View object in JSF
View object in JSF  What is view object
Java object clone
Java object clone  What is criteria for a object to get cloned
How to disable an HTML object?
How to disable an HTML object?  How to disable an HTML object
assign object properties
assign object properties  How do you assign object properties
Date object using JavaScript
Date object using JavaScript  What's the Date object using JavaScript
Potential leak of an object XCode
Potential leak of an object XCode  What is potential leak of an object XCode
implementation object in C
implementation object in C  how to create and implement an object in C
Java object's lock
Java object's lock  What is an object's lock and which object's have locks
Object relational Mapping
Object relational Mapping  Define ORM
Object creation - Ajax
Object creation  How we want to create an object that directly get data from server
create a new object in JavaScript
create a new object in JavaScript  How do you create a new object in JavaScript
create a new object in JavaScript
create a new object in JavaScript  How do you create a new object in JavaScript
JSP Out Object
JSP Out Object  JSP Out Object?   out object denotes... of the object out is jsp.JspWriter. The out object "Javax.servlet.jsp.JspWriter". Methods of out Object: clear() clearBuffer() flush() isAutoFlush() getBufferSize
conver object to int
conver object to int  i have a method returns object but i need int how can convert ? notice:object not means string means object string str map.get(str) returns object but i need an integer
JSP Response Object
JSP Response Object  JSP response Object ?   The response object denotes the HTTP Response data. The result or the information of a request is denoted with this object. The response object handles the output
JSP Application Object
JSP Application Object  JSP Application Object?   Application Object is used to share the data with all application pages. Thus, all users share information of a given application using the Application object
java object class methods
java object class methods  What are the methods in Object class?  There are lots of methods in object class. the list of some methods are as- clone equals wait finalize getClass hashCode notify notifyAll
Insert an object with the add(Object) method
Insert an object with the add(Object) method       In this section you will learn to insert an object at the end of a list using the add(Object) method. Here is an example that provides
object creation - Java Beginners
object creation  I need object creation in depth(with stack,pc registers).Any one can provide me if possible with video/audio
JSP Session Object
JSP Session Object  JSP Session Object?   Session Object... or the interface name of the object session is http.HttpSession. The object session is written as: Javax.servlet.http.httpsession. The Session Object provides
stdclass object php - PHP
stdclass object php  I know that stdClass is an object in PHP. But can you please explain it? it will be really helpful if you can illustrate it with an example
object oriented programming protocol
object oriented programming protocol  What is object oriented programming protocol? Is it different from objective c protocol programming?? Thanks
Object Oriented Programming in PHP
Object Oriented Programming in PHP  Hi, Which version of PHP supports OOPS. How can I learn Object Oriented Programming in PHP? Thanks
Object in java - Java Beginners
Object in java  Hi deepak, I am new to java, can u tell me what is class ,object and instance with example. For example : class raja , raja r = new raja(); ,here raja is class and r is instance . which is object. Object
object creation - Subversion
object creation in Java  In how many ways we can create objects in Java
request object value
request object value  Hi Friends I am developing a web application... request object value for whole application. Problem is..envirement session,application object are not supported by this envirement. then how can i maintain all
Map java Object to database.
Map java Object to database.  How do you map Java Objects with Database tables
JSP Request Object
JSP Request Object  JSP Request Object ?   request object... an HTTP request. The request object is used to take the value from the client?s... of the object request is http.httpservletrequest. The object request is written
java object - Java Beginners
of objects. The primitive data type and keyword void is work as a class object. Object: Object is the basic entity of object oriented programming language... their objects. Object is an instance of the class. It takes the properties (variables
object - Java Interview Questions
?   Hello, i know only two wayes of object instansiations 1.... Thankx   Hi Friend, Yes we can create an object in the following... of creating an object. ClassName classObject = new ClassName(); 2. Using
Object serialization - Java Beginners
Object serialization  Design classes for Currency, Rupee, and Dollar. Write a program that randomly generates Rupee and Dollar objects and write them into a file using object serialization. Write another program to read
 

 

 

DMCA.com