Please bear with me, this is my first post So, I have this superclass called A. And I've 2 subclasses A1 and A2. A has attribute name. A has function toString(). A1 inherits name, and has attribute hair. A1 has a method toString() which goes: string A1::toString(){ string stringA= A::toString(); stringstream info; info<<stringA<<" "<<hair; return info.str(); } A2 is the same as A1, except that it has face instead of hair. it's toString goes info<<stringA<<" "<<face; In my main(), vector<A*> vector; Then, I have a function to add objects void addA1 (vector<A*> &vector) void addA2 (vector<A*> &vector) both use push_back, …

Member Avatar
Member Avatar
+0 forum 2

<? super Animal> means animal or it's superclass only. Then why inside the method it's allowing to add Dog object even though it's a subclass of Animal? This program compiles and runs! import java.util.*; class Cat {} class Animal{Animal() {System.out.println("Animal constructor");}} class Dog extends Animal{Dog() {System.out.println("Dog constructor");}} public class GenericDemo5 extends Animal{ public static void main(String r[]) { List l1 = new ArrayList(); //can add anything since no type here l1.add(new Dog()); l1.add(new Animal()); l1.add(30); met(l1); System.out.println(l1); } public static void met(List<? super Animal> l2) { l2.add(new Animal()); l2.add(new Dog()); System.out.println(l2); }} Even with Generic Instantiation(shown below) same output!! This …

Member Avatar
Member Avatar
+0 forum 11

General rules is that: 1) A private method cannot be overridden. 2) Subclass method should be a lower access modifier than a overridden superclass method. But the output of the program is "I am Son". Why? Is it because of the rule 2 the output comes? If that's the case here, then two rules of overriding I mentioned above are clashing, aren't they? Help please. Thanks.

Member Avatar
Member Avatar
+0 forum 8

Hi Can any one tell me how to call a subclass method from a superclass? For example: I have a class A,B and C. Now B and C extends A. So is there any way that we can call a method of B in A? Thanks Abhishek

Member Avatar
Member Avatar
+0 forum 4

I'm getting a mismatch error and I've looked up why it would appear and I'm confused because what I'm doing doesn't match the error cause. Plus I've followed the instructions from a guide that I'm using. The code on irb... >> class Cat >> attr_accessor :name, :age, :gender >> end => nil >> class Pets >> attr_accessor :name, :age, :gender >> end => nil >> class Cat < Pets >> end TypeError: superclass mismatch for class Cat from (irb):24 any thoughts? thanks.

Member Avatar
Member Avatar
+0 forum 3

Basically this program uses an abstract class which 3 other classes extends to so that in the end an overridden toString() method will print out how many pages must be read for each class. Okay, well this program is for a class, but I am kinda of stuck because my teacher wants me to test the Homework methods below with ArrayList, but I cannot reference is from a static context.My teacher wasn't specific about the tester. All he said was to use Arraylist to test the classes. So I assume it was 4 different arraylist because there is 4 different …

Member Avatar
Member Avatar
+0 forum 3

Hi there. I am currently trying to get my head around using classes across two files. I currently have a super class and a sub class stored in a file and the main function in another. When I try and pass arguments to the class they work fine if the values are int's. However, what I want to pass are book names. Here is the code. [CODE]//publication.cpp #include <iostream> #include <string> using namespace std; //the publication super class class publication { private: char title; //the title of the book char genre; //the genre of the book double date; //the date …

Member Avatar
Member Avatar
+0 forum 3

I just wanted to demonstrate a quick point regarding Constructors & inheritance. Look at these two classes and think about what you might expect to see after the main method has run. Notice that ClassB is a subclass (inherits from) of ClassA. So, what do we expect the output to be? Here's the output: run: Class A constructor Class B constructor BUILD SUCCESSFUL (total time: 0 seconds) As you can see, the main method makes an explicit call to ClassB's constructor, but not ClassA's constructor. ClassA's constructor, is, however called implicitly because ClassB is a subclass of ClassA. An important …

Member Avatar
Member Avatar
+3 forum 6

Hello folks I'm trying to do my assignment and I'm really stuck in! I have to classes..Customer and Pensioner (Customer = Super class and Pensioner = Subclass) The Pensioner has a PensionNo extra than Customer class. I have to make a array of Customer class (for 10) and put Customer and Pensioner in it. Then I have to override << operator and print out the values of Customer/Pensioner in a file. Customer.h [CODE] #ifndef CUSTOMER_H #define CUSTOMER_H #include <iostream> #include <fstream> #include <string> using namespace std; class Customer { public: Customer(string _firstname, string _lastname, string _telephone, int _noofcall); friend ostream& …

Member Avatar
Member Avatar
+3 forum 8

The End.