English, asked by jagdishprajapati, 1 year ago

public class A {
public void f1() {
System.out.println(“A—>f1”);}}

public class B extends A{
public void f1(){
System.out.println(“B—->f1”);}}

public class Test {
public static void main(String[] args) {
A b = new B();
B a1 = new A();
b.f1();
a1.f1();
} }

Answers

Answered by kvnmurty
0
Java program. 

The given program gives a Compilation error for the line:

             B a1 = new A() ;

There are two public classes A and its descendent class B defined. The function f1 defined in class B overrides the definition in class A.

The output of the given C++ program is probably :

Object pointer b is actually pointing to an instantiated object of class B.

Perhaps the statement  B a1 = new A() gives an error.   
 
The pointer to newly instantiated object A cannot be assigned to pointer of class B.  However, in the line above   pointer to newly created object of class B , can be assigned to a pointer of class A.

  The reason is that  pointer to a higher class (ascendent class) can be assigned a pointer to a descendent class.  But pointer to the descendent class cannot be given or assigned the value of a pointer to an object of ascendent class.

  This rule is generally true in all OOPS. Object oriented languages.

kvnmurty: :-)
Answered by anuj
2
ERRORS IN YOUR PROGRAM ARE ::

         System.out.println(“A—>f1”);


           System.out.println(“B—->f1”);
 CORRECT THESE AND TELL WHAT U WANT ???
    
Similar questions