adding subclass to the superclass array in

Posted on 2012-09-03
Last Modified: 2012-09-03

I am trying to add subclasses object to the superclass array in
Can't find a good example.

I want to do this in
public class Animal {
     public int height;
     public int width;
public class Dog extends Animal {
    public int weight;
public class Cat extends Animal {
    public String color;

Public class MyMain {

Cat myCat = new Cat();
        myCat.height = 10;
        myCat.width = 20;
        myCat.weight = 300;
        Dog myDog = new Dog();
        myDog.width = 200;
        myDog.color = "yellow";
        Cat cat1= new Cat();
        Dog dog1= new Dog();

Animal[] animals;
        animals = new Animal[3];
        System.out.println("cat " + animals[0].height);
        System.out.println("cat " + animals[0].width);
        if (animals[0] instanceof Cat) {
            cat1 = (Cat) animals[0];
            System.out.println("cat " + cat1.weight);
        System.out.println("dog " + animals[1].height);
        System.out.println("dog " + animals[1].width);
        if (animals[1] instanceof Dog) {
            dog1 = (Dog) animals[1];
            System.out.println("dog " + dog1.color);
Question by:dkim18
    LVL 53

    Assisted Solution

    Just tested with the following c# console-app

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication5
        class Program
            static void Main(string[] args)
                Cat myCat = new Cat();
                myCat.Height = 10;
                myCat.Width = 20;
                myCat.Color = "yellow";
                Dog myDog = new Dog();
                myDog.Height = 100;
                myDog.Width = 200;
                myDog.Weight = 300;
                Animal[] animals;
                animals = new Animal[3];
                animals[0] = myCat;
                animals[1] = myDog;
            Console.WriteLine("cat " + animals[0].Height);
            Console.WriteLine("cat " + animals[0].Width);
            if (animals[0].GetType() == typeof(Cat))
                Console.WriteLine("cat " + ((Cat) animals[0]).Color);
            if (animals[0].GetType() == typeof(Dog))
                Console.WriteLine("cat " + ((Dog)animals[0]).Weight);
        public class Animal {
         public int Height;
         public int Width;
        public class Dog : Animal 
            public int Weight { get; set; }
        public class Cat :Animal 
            public String Color;

    Open in new window

    LVL 74

    Accepted Solution

    Dhaest's example in VB:

    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Namespace ConsoleApplication5
    	Class Program
    		Private Shared Sub Main(args As String())
    			Dim myCat As New Cat()
    			myCat.Height = 10
    			myCat.Width = 20
    			myCat.Color = "yellow"
    			Dim myDog As New Dog()
    			myDog.Height = 100
    			myDog.Width = 200
    			myDog.Weight = 300
    			Dim animals As Animal()
    			animals = New Animal(2) {}
    			animals(0) = myCat
    			animals(1) = myDog
    			Console.WriteLine("cat " + animals(0).Height)
    			Console.WriteLine("cat " + animals(0).Width)
    			If animals(0).[GetType]() = GetType(Cat) Then
    				Console.WriteLine("cat " + DirectCast(animals(0), Cat).Color)
    			End If
    			If animals(0).[GetType]() = GetType(Dog) Then
    				Console.WriteLine("cat " + DirectCast(animals(0), Dog).Weight)
    			End If
    		End Sub
    	End Class
    	Public Class Animal
    		Public Height As Integer
    		Public Width As Integer
    	End Class
    	Public Class Dog
    		Inherits Animal
    		Public Property Weight() As Integer
    				Return m_Weight
    			End Get
    				m_Weight = Value
    			End Set
    		End Property
    		Private m_Weight As Integer
    	End Class
    	Public Class Cat
    		Inherits Animal
    		Public Color As [String]
    	End Class
    End Namespace
    'Service provided by Telerik (
    'Conversion powered by NRefactory.
    'Twitter: @telerik, @toddanglin

    Open in new window


    Author Comment

    THanks. This is what I am looking for.

      if (animals[0].GetType() == typeof(Cat))
    If animals(0).[GetType]() = GetType(Cat) Then
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Just personal preference...but I'd write it as:

            If TypeOf animals(0) Is Cat Then
                Console.WriteLine("cat " + DirectCast(animals(0), Cat).Color)
            End If

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now