superclass

Java: Superclass to construct a subclass on certain conditions, possible?

I have this condition public class A { public action() { System.out.println("Action done in A"); } } public class B extends A { public action() { System.out.println("Action done in B"); } } when I create an instance of B, the action will do just actions in B, as it overrides the action of the su...

Reflection: cast an object to subclass without use instaceof

I have this simple interface/class: public abstract class Message { } public class Message1 extends Message{ } public class Message2 extends Message{ } And an utility class: public class Utility { public void handler(Message m){ System.out.println("Interface: Message"); } public void handler(Message1 m){ Syste...

Is it possible to use inheritance in this situation? (Java)

I have ClassA and ClassB, with ClassA being the superclass. ClassA uses NodeA, ClassB uses NodeB. First problem: method parameters. ClassB needs NodeB types, but I can't cast from the subclass to the superclass. That means I can't set properties which are unique to NodeB's. Second problem: When I need to add nodes toClassB, I have to ...

Use Automapper to flatten sub-class of property

Given the classes: public class Person { public string Name { get; set; } } public class Student : Person { public int StudentId { get; set; } } public class Source { public Person Person { get; set; } } public class Dest { public string PersonName { get; set; } public int? PersonStudentId { get; set; } } I want...

Use of Java [Interfaces / Abstract classes]

Hello, Lately i decided to take a look at Java so i am still pretty new to it and also to the approach of OO programming, so i wanted to get some things straight before learning more, (i guess it's never to soon to start with good practices). I am programming a little 2D game for now but i think my question applies to any non trivial p...

How do you call a method for an Objective-C object's superclass from elsewhere?

If you're implementing a subclass, you can, within your implementation, explicitly call the superclass's method, even if you've overridden that method, i.e.: [self overriddenMethod]; //calls the subclass's method [super overriddenMethod]; //calls the superclass's method What if you want to call the superclass's method from somewhere o...

What's my best approach on this simple hierarchy Java Problem?

First, I'm sorry for the question title but I can't think of a better one to describe my problem. Feel free to change it :) Let's say I have this abstract class Box which implements a couple of constructors, methods and whatever on some private variables. Then I have a couple of sub classes like BoxA and BoxB. Both of these implement ex...

inheritance problem OOP extend

If a Father is a Parent and a Parent is a Person and a Person has a Father I create the following: class Person{ Father father; } class Parent extends Person{} class Father extends Parent{} Instances: Person p1 = new Person(); Person p2 = new Person(); p1.father = p2; //father is of the type Father This doesn't work... Now tr...

Invoking a superclass's class methods in Python

I am working on a Flask extension that adds CouchDB support to Flask. To make it easier, I have subclassed couchdb.mapping.Document so the store and load methods can use the current thread-local database. Right now, my code looks like this: class Document(mapping.Document): # rest of the methods omitted for brevity @classmethod de...

Question regarding subclassing.

Hi, This is probably asked before but I have no idea what to search for. This is my hierarchy now: NSObject > FirstSubclass > SecondSubclass. But I'm going to implement a new feature in my app which requires changing a few details in FirstSubclass when a certain condition is met. So actually I would need a subclass between FirstSubclass...

Checking programmatically if a .class file extends particular class.

Hi, I have a problem I've been trying to solve for quite some hours. In an Eclipse plugin, I have an ArrayList that contains the full paths (as strings) of some java .class files. What I'd like to do is check if the classes that are included in the list extend a particular class. I thought about parsing the file, looking for what is afte...

Can I change the super of a class?

Hi, Is it somehow possible to choose the super of a class (preferably in the alloc or init method) so my class inherits from something else? ...

When to implement an interface and when to extend a superclass?

I've been reading a lot about interfaces and class inheritance in Java, and I know how to do both and I think I have a good feel for both. But it seems that nobody ever really compares the two side by side and explains when and why you would want to use one or the other. I have not found a lot of times when implementing an interface woul...

Getting the name of a sub-class from within a super-class.

Let's say I have a base class named Entity. In that class, I have a static method to retrieve the class name: class Entity { public static String getClass() { return Entity.class.getClass(); } } Now I have another class extend that. class User extends Entity { } I want to get the class name of User: System.out.pri...

Cloneable behaviour

Java doc says - The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time. Which is why clone method in Object class is protected ? is that so ? That means any class which doesn't implement cloneable w...

How can I parse parameters in subclass before calling constructor of the superclass?

public Subclass(String[] parameters) throws IllegalArgumentException { super("Rectangle", Double.parseDouble(parameters[0]), Double.parseDouble(parameters[1]), 90, Double.parseDouble(parameters[2]), Double.parseDouble(parameters[3])); if(parameters.length != ...

Classes Superclasses Subclasses

I'm trying to make two subclasses a class: // Old code - (void)setPaging { [pagingScrollView addSubview:self.ImageScrollView]; } @interface ImageScrollView : UIScrollView <UIScrollViewDelegate> { UIView *imageView; NSUInteger index; } @property (assign) NSUInteger index; - (void)displayTiledImageNamed:(CGPDFPage...

C++ superclass Array yet access subclass methods?

i have an accounts class from that i have 3 types of accounts savings, credit, and homeloan. i created a binary search tree to hold all the accounts as type account how do i now access the methods of the subclasses depending on the type of object? have resolved all errors with syntax and codeing but this. been racking my head for 2 d...

Why aren't Python's superclass __init__ methods automatically invoked?

Why did the Python designers decide that subclasses' __init__() methods don't automatically call the __init__() methods of their superclasses, as in some other languages? Is the Pythonic and recommended idiom really like the following? def Superclass(object): def __init__(self): print 'Do something' def Subclass(Superclass)...

Adding new methods to superclasses and resulting problems -Likelihood?

Item 16 of Effective Java 2nd edition, favor composition over inheritance says the following "If the superclass acquires a new method in a subsequent release and you have the bad luck to have given the subclass a method with the same signature and a different return type, your subclass will no longer compile. If you’ve given the subcla...