0% found this document useful (0 votes)
28 views

Object Creation in Java.: A B B A A B A B

There are four ways to create objects from classes A and B, where B extends A. Objects can be created using the class type (ob1 and ob3), accessing methods defined in that class and superclasses. Objects can also be created using the subclass (ob2), allowing access to overridden and own methods. An object cannot be created as the superclass using the subclass type (ob4).

Uploaded by

Roshan Chan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Object Creation in Java.: A B B A A B A B

There are four ways to create objects from classes A and B, where B extends A. Objects can be created using the class type (ob1 and ob3), accessing methods defined in that class and superclasses. Objects can also be created using the subclass (ob2), allowing access to overridden and own methods. An object cannot be created as the superclass using the subclass type (ob4).

Uploaded by

Roshan Chan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

OBJECT CREATION IN JAVA.

In the following example we have classes a and b. b extends a. In other words a is


the super class and b is the subclass. The following program shows FOUR
possibilities of creating objects of the classes a and b and the accessibility of the
objects.
package exercise;
public class Exercise {
public static void main(String[] args) {
a ob1=new a();
b ob2=new b();
a ob3=new b();
// a ob4=new b(); NOT ALLOWED
run:
ob1.f();
ob1.fa();

f In class A
fa In class A

ob2.f();
ob2.fa();
ob2.fb();

f In class B
fa In class A
fb In class B

ob3.f();
ob3.fa();
//ob3.fb();

f In class B
fa In class A
NOT ALLOWED

(total time: 0 seconds)

}
package exercise;

package exercise;

public class a {
void fa()
{
System.out.println("fa In class
A");
}

public class b extends a{


void fb()
{
System.out.println("fb In class B");
}

void f()
{
System.out.println("f In class
A");
}
}

void f()
{
System.out.println("f In class B");
}
}

You might also like