0

I am so confused in this topic. //a class (lets say B) extendes or implements another class or interface respectively

interface myInterfaceA{

int interfaceDataMember1;
int interfaceDataMember2;

void interfaceMethod1();
void interfaceMethod2();

}

class myClassA{

int parentClassDataMember1;
int parentClassDataMember2;

myclassA(){}

void parentClassMethod1(){}
void parentClassMethod2(){}

}

//case1

class B implements myInterfaceA{

int dataMember1;
int dataMember2;

B(){}

void method1(){}
void method2(){}
}

// OR case2

class B extends myClassA{

int dataMember1;
int dataMember2;

B(){}

void method1(){}
void method2(){}
}

// so in either case what is the purpose of creating the object of class B in the following way

myInterfaceA objectB = new B();
// or 
myClassA objectB = new B();

1) is there any name of this procedure? 2) what (data memeber, methods, constructor ) will be loaded in objectB? 3) if all the code of class B will be loaded in the objectB then why did we give the refrece of interface or parent class? 4) is this shows polymorphism? if yes, then why? 5) in case2 will class B also inherit the constructor of myClassA 6) why the constructor of parentclass is also called whe we create child class object

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zoha
  • 3
  • 4

3 Answers3

1

1) is there any name of this procedure?

This is polymorphism.

2) what (data memeber, methods, constructor ) will be loaded in objectB?

Every data member and method will be inherited by the objectB.

In case of interfaces, the data members are private, static, final constants. They must be initialized in the constructor. The methods must be implemented by the class B.

In case of superclasses, the data members and methods are simply inherited. You can override the methods. Variables are not polymorphic.

3) if all the code of class B will be loaded in the objectB then why did we give the refrece of interface or parent class?

We give reference of interface or parent class so that in case of multiple subtypes, we can have a single method that accepts supertype instead of creating multiple methods. This reduces lines of code and makes the code readable.

4) is this shows polymorphism? if yes, then why?

This shows polymorphic behaviour so you don't need to bind each subtype to a different method. A single method can be written to dynamically bind all the subtypes of a single supertype.

5) in case2 will class B also inherit the constructor of myClassA

The constructor is not inherited. You must call explicitly super() if required.

6) why the constructor of parentclass is also called whe we create child class object

It is not mandatory to call the constructor of the parentclass everytime. You may skip it if it is not required. But as a standard practice, super() is the first line of the child class constructor, so that any changes in the super class object creation does not affect child class.

  • Thanks alot for help. but didnt get it "any changes in the super class object creation does not affect child class" – Zoha Aug 07 '16 at 11:04
0

Interfaces (and implementing them) only dictate what method signatures the inheriting class must have. The only thing that is 'copied', ie available as methods, are default methods since Java 8

Extending from a class (or abstract class) is a whole different story, although it also can dictate what method signatures are to be implemented by the inheriting class. But here, all data is not copied, but available, to the calling interface.

Interfaces are used to standardise behaviour (treat dogs and birds as pets), abstract classes to standardise behaviour AND provide implementations (let budgie and cockatoo fly)

package zoo;

import java.util.ArrayList;



interface Pet {
    void printName();
}



abstract class Bird implements Pet {
    public void fly() {
        System.out.println("I (" + getClass().getSimpleName() + ") am flying");
    }
}



class Dog implements Pet {
    @Override public void printName() {
        System.out.println("Hans");
    }
}



class Budgie extends Bird {
    @Override public void printName() {
        System.out.println("Jockl");
    }
}



class Cockatoo extends Bird {
    @Override public void printName() {
        System.out.println("Zenzi");
    }
}



public class AnimalSchool {
    public static void main(final String[] args) {
        final Dog d = new Dog();
        d.printName();

        final Budgie b = new Budgie();
        b.printName();
        b.fly();

        final Cockatoo c = new Cockatoo();
        c.printName();
        c.fly();

        final ArrayList<Pet> pets = new ArrayList<>();
        pets.add(d);
        pets.add(b);
        pets.add(c);
        for (final Pet pet : pets) {
            System.out.print("\nPet is a " + pet.getClass().getSimpleName() + " and is called ");
            pet.printName();
        }

        final ArrayList<Bird> byrdies = new ArrayList<>();
        //      byrdies.add(d); this will not compile, as it is not a bird
        byrdies.add(b);
        byrdies.add(c);
        for (final Pet pet : byrdies) {
            System.out.print("\nBird is a " + pet.getClass().getSimpleName() + " and is called ");
            pet.printName();
        }

    }
}
JayC667
  • 2,418
  • 2
  • 17
  • 31
0

To answer your question.

  1. It is called polymorphism
  2. In object B using case 1, B will be forced to implement myInterfaceA methods also your class B cannot have unimplemented methods except you declare the class as abstract thus:

    class B implements myInterfaceA{
    
      int dataMember1;
      int dataMember2;
    
      B(){}
    
      public void method1(){
      }
      public void method2(){
      }
    
      //interface methods
      public void interfaceMethod1(){
      }
      public void interfaceMethod2(){
      }
     }
    

    Hence class b will have properties of the interface and that of itself as well as methods it has implemented. Using case 2 B will however be implemented like this. (Assuming a is not an abstract class, thus it's methods will be implemented not declared like interface methods or abstract class method)

    class B extends myClassA{
    
      int dataMember1;
      int dataMember2;
    
      B(){
        super();
      }
    
      public void method1(){
      }
      public void method2(){
      }
     }
    

    notice that an explicit call may be made to the super class constructor

  3. we give the reference to the interface/parent class because we want to have a single implementation where the super type is passed and the implementation is used for cases where there are many subtypes

  4. Yes, it is polymorphic so that we can have different behaviours and implementations of the super type.

  5. As i said earlier, an explicit call will be made to the super class constructor

  6. It is standard practice to call the superclass constructor so that changes to it will not affect the subclass

Akintayo Jabar
  • 836
  • 8
  • 13