473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Array of Objects for an abstract class

Can we make Array of objects for any abstract class in java....IF YES GIVE AN EXAMPLE....
Oct 5 '07 #1
2 6061
JosAH
11,448 Expert 8TB
Can we make Array of objects for any abstract class in java....IF YES GIVE AN EXAMPLE....
Sure you can; have a look:

Expand|Select|Wrap|Line Numbers
  1. public abstract class Animal { ... }
  2. public class Penguin extends Animal { ... }
  3. public class Gorilla extends Animal { ... }
  4. ...
  5. Animal[] animals = { new Penguin(), new Gorilla() };
  6.  
kind regards,

Jos
Oct 5 '07 #2
Ganon11
3,652 Expert 2GB
But remember that you can only treat each object in the array as a type of the abstract class. So in Jos' example, you have a Penguin and Gorilla, but you can only treat them as Animals. You can change this by casting the object to a new type (i.e. (Penguin)animals[0]), but you can only do this if you know for sure that animals[0] is actually a Penguin.
Oct 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
25
by: CodeCracker | last post by:
Problem details below: I have few items(simple values or objects) to be put into an array and I implement it through a set rather than just an array of items. This is because every time I get a...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
4
by: Terry | last post by:
I'm building some dll assemblies that have in them the implementation of an abstract class defined in a different assembly. I'm trying to create objects of the type defined in the dlls with...
3
by: Pol Bawin | last post by:
Hi All, One : I have a property that get/set a array of an abstract class A By default my array is null In the propertygrid, It is not works correctly when my array is null. (when my array...
3
by: Anders Borum | last post by:
Hello Jon, et all. I am working on a framework with context bound objects (models). The objects expose common functionality, such as the ability to get a serialized Xml representation of an...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
13
by: Gannon56789 | last post by:
Ok i have been trying to figure this out for hours upon hours and i can't get it to work. I have an abstract base class called Employee. From employee i have derived four classes in which i define...
0
by: Gannon56789 | last post by:
I have a class from which i must use a constructor that initializes an array of pointers to objects that are Abstract. I keep getting an error when i try to compile this because i guess it is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.