Call subclass methods?

call subclass method from superclass java
how to call subclass method in java
how to access superclass variables in subclass in java
javascript call subclass method from superclass
superclass reference to subclass object in java
java abstract class call subclass method
subclass java
java inheritance

So, I have this super class:

public class Guest {

    private int id;
    private String firstName;
    private String lastName;
    private LocalDate joinDate;

    public Guest(int id, String firstName, String lastName, LocalDate joinDate){ = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.joinDate = joinDate;

    public int getID(){ return id; }
    public String getFirstName(){ return firstName; }
    public String getLastName(){ return lastName; }
    public LocalDate getJoinDate(){ return joinDate; }

and its sub class

public class VIP extends Guest{

    private LocalDate VIPstartDate;
    private LocalDate VIPexpiryDate;

    public VIP(int id, String firstName, String lastName, LocalDate joinDate, LocalDate VIPstartDate, LocalDate VIPexpiryDate){

        super(id, firstName, lastName, joinDate);

        this.VIPstartDate = VIPstartDate;
        this.VIPexpiryDate = VIPexpiryDate;
    public LocalDate getVIPstartDate(){ return VIPstartDate; }
    public LocalDate getVIPexpiryDate(){ return VIPexpiryDate; }

In another class, I have a list called 'guests' where all the guest information is stored.

How can I call the VIP subclass methods like this? (guests is a list where guest info is stored)

for (Guest guest : guests){
    guest.getid()  // so this calls from the super class - this works
    guest.getVIPstartDate() // this is meant to call from sub class - this doesn't work

^ I can't do this in my code. Why not?

How am I able to do this?


This solution will work.

((VIP) guest).getVIPstartDate();

How to call a subclass method from superclass?, I would like to do fruit.toString() and have it run the Apple.toString() or Banana. toString() method based on what the fruit actually is. You can call the static method of the superclass − Using the constructor of the superclass. new SuperClass().display(); Directly, using the name of the superclass. SuperClass.display(); Directly, using the name of the subclass. SubClass.display(); Example. Following Java example calls the static method of the superclass in all the 3 possible

You instantiated your Guests as the superclass. Without casting there is no way for the software to know you actually want VIP. There's a LOT of different ways to deal with this.

As Andronicus above me mentioned, Casting is the most obvious solution. (It's also generally what similar homework assignments are looking for) BUT you need to be careful here to verify it is actually the right subclass to cast to.

if (guest instanceof VIP){
   ((VIP) guest).getVipStartDate();

Referencing Subclass objects with Subclass vs Superclass , Second approach (Referencing using subclass reference) : A we are unable to call subclass specific methods or access subclass fields. Subclasses can override superclass methods to support the greater specialization defined by the subclass. Because of the relationship that a subclass object is a superclass object, it is often useful to call the superclass version of the method before executing the specialized subclass code.

You would have to cast it first:

((VIP) guest).getVIPstartDate()

How to call subclass method? (Beginning Java forum at Coderanch), When you declare obj as a variable of type SuperA, you only get access to the methods defined in SuperA. You can override these methods in� They can, however, be referenced indirectly via methods reachable from the subclass (e.g default (package), protected and public methods). Constructors are not inherited by subclasses, but a subclass constructor must call a constructor in the superclass. This will be explained in detail in a later section. Java Only Supports Singular Inheritance

This is one of example that probably help for calling subclass 'class Manager extends Employee { . . . public void setBonus(double b) { bonus = b; } private double bonus; } There is nothing special about these methods and fields. If you have a Manager object, you can simply apply the setBonus method.

Manager boss = . . .; boss.setBonus(5000);

Java Inheritance, If your method overrides one of its superclass's methods, you can invoke the Here is the MountainBike (subclass) constructor that calls the superclass� private fields and methods; Constructors. Although, the subclass constructor has to call the superclass constructor if its defined (More on that later!) Multiple classes. Java supports only single inheritance, that is, you can only inherit one class at a time. Fields. Individual fields of a class cannot be overriden by the subclass. Type

Using the Keyword super (The Java™ Tutorials > Learning the Java , Subclass methods can call superclass methods if both methods have the same name. From the subclass, reference the method name and superclass name with � Hello this is the method of the superclass Hello this is the method of the subclass Calling superclass methods from a static context. Yes, you can call the methods of the superclass from static methods of the subclass (using the object of subclass or the object of the superclass). Example. Live Demo

Call Superclass Methods on Subclass Objects, Superclass method calls subclass method: bug or Learn more about overloaded function, superclass, oop MATLAB. Above program shows sub class object using super class methods and variables as we said in inheritance concept all super class members can accessed by the sub class means all super class members are available to sub class if sub class extending super class.

Superclass method calls subclass method: bug or feature? How to , In class A: void myMethod () { B b = new B(); b.theMethodOfB(); }. This also works when B has overridden a method from A. Java Inheritance (Subclass and Superclass) In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class; superclass (parent) - the class being inherited from; To inherit from a class, use the extends keyword.

  • If the objects in guests list are of type Guest then you cannot invoke getVIPStartDate on them because they are not of type VIP but are of type Guest and Guest does not have a method getVIPStartDate. if the objects are of type VIP then you can type cast to VIP and invoke your method on vip object
  • This is the exact repetition of my answer
  • i was intitally trying to edit your answer itself but it was asking for atleast six char change fo edit,
  • And what's the difference?
  • parenthesis, you can check it by running.
  • @Andronicus At least for me, ((VIP) guest). works, (VIP guest). doesn't. Change your answer to that and I'll accept it.