class BaseClass {
private void f() {
System.out.println("Baseclass f()");
}
public static void main(String[] args) {
BaseClass dc = new DerivedClass();
dc.f();
}
}
class DerivedClass extends BaseClass {
public void f() {
System.out.println("DerivedClass f()");
}
}
In my opinion, the object ...
hi guys,
i needed help refactoring the following class,
Following is a class Operation with variety of Operations in switch :
i want to avoid the switch statement.I read few articles on using polymorphism and state pattern .but when i refactor the classes i dont get access to many variables,properties
Im confused on whether to use o...
For SCJP most of the time question such as below is asked to find valid example of polymorphic method calls. But what should one exactly look for to find is it polymorphic use or not ?
abstract class A {
abstract void a1();
void a2() { }
}
class B extends A {
void a1() { }
void a2() { }
}
class C extends B {
void ...
I have two classes: a base class, Foo::Base and a derived class, Foo::Base::Sub. I want to have Foo::Base::Sub do some type and data checking on the constructor`s argument--a hash--before blessing it. I've tried overriding Foo::Base->new's constructor, doing the checks and then calling Foo::Base->new (since the code would be exactly th...
I don't know if a array is really a object on php ... but, what I want is to change the array behavior on a foreach loop.
Something similar to this on java:
for( String it : myArr = new Array_Iterator( array) implements Iterator{
public Array_Iterator( String[] arr){ this.arr = arr}
/* interface implementation */
}){
/* loo...
Say I have abstract base class Animal then Dog and Cat classes. I'm mapping these via table-per-subclass (w/ discriminator). Is it possible to query Animals and also project the discriminator-value or class name?
It seems I cannot project the discriminator column. I know when I list Animals, NHibernate creates the correct Animal type f...
I'm trying to set parameters for a abstract class:
public abstract class NewMath {
public abstract int op (int intOne, int intTwo);
}
Here is the extended subclass:
public class MultMath extends NewMath {
public int op (int intOne, int intTwo){
return intOne + intTwo;
}
}
But when I try to instantiate an object ...
I have a group of classes (say for validation rules). Each one returns a true or false.
I use id and call a method signature for each one of the classes and get the results allowing me to dynamically create validation rules.
Worked great until...
I have a new class that takes an extra parameter to come up with its validation.
What i...
Consider the following code:
public abstract class Base {
public void getAnswer();
}
public class Derived1 extends Base {
public void getAnswer() {
}
}
public class Derived2 extends Base {
public void getAnswer() {
}
}
public class Main {
public final int DERIVED1 = 1;
public final int DERIV...
I have class X, an abstract class, and classes A and B that inherit from it. Classes A and B each have their own 'return_something' function. I have another method elsewhere that calls 'return_something' on a series of objects, all of type X. 'return_something' returns something different depending on whether it is an A or a B, so I c...
I have three tables: "users", "courses" and "documents". I would like to upload documents to users as well as to courses, so I would need two belongsTo-relations for the Document model. Some belong to one model, some to the other.
Is there a simple solution to construct these relations?
How could I set up the "add"-actions?
I know I c...
As you can see in the code below, I have an Abstract Base Class "HostWindow", and class that derives from it "Chrome". All the functions are implemented in Chrome. The issue is, I can't call functions in Chrome if they're virtual.
class HostWindow : public Noncopyable {
public:
virtual ~HostWindow() { }
// Pure virtual function...
I'm beginning work on a new project that's would be much easier if there was some way to make different data models polymorphic. I'm looking at using the Entity Framework 4.0 (when it's released), but have been unable to determine if it will actually be able to work.
Here's the basic scenario. I'm implemented a comment system, and wou...
class A
{
public:
virtual void
doSomething(void)
{}
void
doStuff(void)
{
doSomething();
}
};
class B : public A
{
public:
void
doSomething(void)
{
// do some stuff here
}
};
B * b = new B;
b->doStuff();
It gives me Segmentation fault. What am I doing wrong? It s...
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
class Helper
{
public:
Helper() { init(); }
virtual void print() {
int nSize = m_vItems.size();
std::cout << "Size : " << nSize << std::endl;
std::cout << "Items: " << std::endl;
for(int i=0; i<nSize; i++) {
s...
I am new to OOP. Though I understand what polymorphism is, but I can't get the real use of it. I can have functions with different name. Why should I try to implement polymorphism in my application.
...
I'm working with a smallish type hierarchy, something like the following, and lets say there won't ever be any other Animal types in my sad safari-less world (I'm not at all worried about resilience to expanding):
public abstract class Animal {};
public sealed class Dog : Animal {};
public sealed class Cat : Animal {};
public sealed cla...
I'm using an API that has a "Member" class. I wish to extend this so i have create "MemberProfile" which inherits from "Member"
I have some issue creating the constructor for this class. I wish to something like the following
var member = Member.GetCurrentMember();
var memberProfile = new MemberProfile(member);
How would the...
How it is determined whether the below call is bound at compile time or at runtime?
object.member_fn;//object is either base class or derived class object
p->member_fn;//p is either base class or derived class pointer
EDITED:
#include <iostream>
using namespace std;
class Base
{
public:
Base(){ cout<<"Constructor: Ba...
I have been reading Nhibernate in Action, but the section on mapping polymorphic collections is a little too short on how to do this.
I have the following code
[Class]
[Discriminator(Column="MachineType",TypeType=typeof(string))]
public abstract class Machine
{
[Property]
public string Name{get;set;}
}
[Subclass(DiscriminatorValue...