PsyDuck
PsyDuck

Reputation: 99

Declare a class in java

Im a beginner. I have a issue about class in java. So I have two class Father and class Son extends class Father

class Father
{
   Father(){};
   public void SayHi()
   {
       System.out.println("Hello world");
   }
}
class Son extends Father
{
   Son(){}
   public void SayHiToo()
   {
       System.out.println("Hello world, too!");
   }

}
class DO
{
    public static void  main(String[] args)
    {
          Father c1 = new Father();     //(1)
          Son c2 = new Son();           //(2)
          Father c3 = new Son();        //(3)
    }
}

My question is about: I know that (1),(2) mean c1 can only use functions in class Father and c2 can use functions in class Son and Father But if I declare c3 like this, what is it mean? Thanks very much

Upvotes: 1

Views: 100

Answers (4)

S. Plekhanov
S. Plekhanov

Reputation: 21

A Father reference can always refer to a Son instance, because Son IS-A Father. What makes that supertype reference to a subtype instance possible is that the subtype is guaranteed to be able to do everything the supertype can do. Whether the Son instance overrides the inherited methods of Father or simply inherits them, anyone with an Father reference to a Son instance is free to call all accessible Father methods. For that reason, an overriding method must fulfill the contract of the superclass.

Upvotes: 0

azro
azro

Reputation: 54148

With Father c3 = new Son();, the c3 can only use the methods of Father, but as it's a Son , you can cast it as a Son to use also Son's method

Father c3 = new Son();
c3.SayHi();
Son sc3 = (Son) c3;
sc3.SayHiToo();

But be careful at your model : is a Son a type of Father ? No.

For example :

  • a Car is a Vehicule
  • a Smartphone is a Phone

Upvotes: 0

Jesper
Jesper

Reputation: 206876

This will become much more clear if you give your classes better names.

An important thing to remember is: inheritance means specialization: a subclass is a special kind of its superclass.

Now you see why class Father and class Son extends Father are really wrong: a Son is not a (special kind of) Father.

Instead of Father and Son, let's use Animal and Cat:

class Animal {
    public void makeSound() {
        System.out.println("Beep!");
    }
}

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Miaow!");
    }
}

Now it's easier to understand what these mean:

Animal c1 = new Animal(); // 1
Cat c2 = new Cat();       // 2
Animal c3 = new Cat();    // 3

Cat c4 = new Animal();    // 4 WRONG!

A Cat is an Animal, so in (3) it is allowed to assign a Cat object to a variable of type Animal - Cat is guaranteed to have all methods that are available on Animal. So, any method you could call on c3 exists in the object that c3 refers to.

The other way around, assigning an Animal to a variable of type Cat as in (4), is not allowed, since class Cat might have extra methods that you can't call on an Animal object. If (4) was allowed, you could try to call those methods, which should not be possible.

Upvotes: 3

TapaGeuR
TapaGeuR

Reputation: 76

It's like instanciating a Son that will act like a Father!

I would suggest you reading about java polymorphism.

Upvotes: 2

Related Questions