Wat does it mean by indirect Instantiation of abstract class ? how do
we achieve this ?
as i tried few times like .. it gives error has any one done something regarding this
abstract class hello //abstract class declaration
{
void leo() {}
}
abstract class test {} //2'nd abstract class
class dudu { //main class
public static void main(String args[])
{
hello d = new test() ; // tried here
}
}
We can't instantiate an abstract class .If we want than we have to extend it.
You can't instantiate an abstract class. The whole idea of Abstract class is to declare something which is common among subclasses and then extend it.
public abstract class Human {
// This class can't be instantiated, there can't be an object called Human
}
public Male extends Human {
// This class can be instantiated, getting common features through extension from Human class
}
public Female extends Human {
// This class can be instantiated, getting common features through extension from Human class
}
For more: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
Wat does it mean my indirect instanciation of abstract class ? how do we achieve this ?
I'd need to see the context in which that phrase is used, but I expect that "indirect instantiation" means instantiation of a non-abstract class that extends your abstract class.
For example
public abstract class A {
private int a;
public A(int a) {
this.a = a;
}
...
}
public B extends A {
public B() {
super(42);
}
...
}
B b = new B(); // This is an indirect instantiation of A
// (sort of ....)
A a = new A(99); // This is a compilation error. You cannot
// instantiate an abstract class directly.
You can't create instance of abstract class, I think this is what you are trying to do.
abstract class hello //abstract class declaration
{
void leo() {}
}
class test extends hello
{
void leo() {} // Custom test's implementation of leo method
}
you cannot create object for Abstract class in java.
Refer this link-http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
Related
I am new to Java. I have been researching the notion of an abstract class.
I understand that abstract classes cannot be instantiated. So an abstract subclass could not be instantiated with new in the abstract base class.
However, I have been looking for a driver/main class that is abstract to study since instantiating is not allowed.
Example:
public abstract class Research{
public static void main(String [] args){
[Code here
}
}
Can anyone please explain what a driver/main class for an abstract class would look like?
Thank you.
An abstract class cannot be instantiated
public abstract class Person {
// hi i am an abstract person
// put your abstract methods here
}
public class Student extends Person {
// This class provides the implementation of the abstract methods in the Person abstract class
// implement the abstract methods of Person
}
public class Driver {
public static void main(String[] args) {
// Person p = new Person(); // wrong
Student s = new Student();
}
}
See https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html it explains in detail abstract classes in Java.
The word ABSTRACT should hint you that it is not real. That's why you cannot touch a real instance of that class(like interface). But asbtract class can have some method implementations(interface does not have). In order to test already implemented metods just extend that class with some stubs and test already implemented methods.
Lets say
abstract class AbstractClass{
implementedMethod(){ //some code}
abstractMehod()
}
class ExtendedClass extends AbstractClass{
abstractMethod(){//implementation}
public static void main(String args[ ]) {
//here you can drive
}
}
package corejava;
abstract class abstractA // abstract class A
{
abstract void abst(); // Abstarct Method
void eat() // Non abstract method
{
System.out.println("non abstract");
}
}
class B extends abstractA // class B extends abstract Class
{
#Override // define body of abstract method
void abst() {
System.out.println("abstract method define");
}
void eat() // override eat method
{
System.out.println("non abstract override ");
}
}
public class alloops { // Main class
public static void main(String[] args)
{
B b=new B(); // create Object B Class
abstractA a = new abstractA() { // create Object of abstract Class
#Override
void abst() {
System.out.println("again abstract");
}
};
a.eat(); //instance of abstract class
System.out.println(a instanceof abstractA);
b.abst();
b.eat();
a.abst();
}
}
Output:
non abstracttrue,abstract method definenon abstract override again abstract
In this case output is above. I want to know if it's right or wrong. Do I have have to create an instance of the abstract class or not?
What you have done is creating an anonymous inner class.
abstractA a = new abstractA() { // create Object of abstract Class
#Override
void abst() {
System.out.println("again abstract");
}
};
abstractA a = new abstractA() {
#Override
void abst() {
System.out.println("again abstract");
}
};
The above code actually defined an anonymous class extending your abstract class. Your code works as it's expected.
You can't create an instance of abstract class.You are creating an instance of anonymous inner class which is extending your abstract class. Confusion is creating because this line
is returning true.
a instanceof abstractA
instanceof is used to check if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
In this case abstractA is an instace of abstract class's
subclass.
Output will be more clear if you use these lines to determine class types of instances
System.out.println(b.getClass().toString());
System.out.println(a.getClass().toString());
Output:
class corejava.B
class corejava.alloops$1
First line of the output tells that
b
is an instance of class B in package corejava
Second line tells that a is the first anonymous inner class ($1) of class alloops of package corejava
I had a doubt.
Imagine If we have a class A that implements the method
For example
private void methodA(int index) throws Exception, Error {
}
And if we have a Class B that extends the first class A.
My questions is, can class B implement
private void methodA(int index) throws Exception, Error {
}
And which method will be called under which circumstance!!
Thanks
If your methods weren't declared "private", this would just be standard polymorphism. Because they're private, the rules are a bit different. The version in class A can only be called from code that's in class A. The version in class B can only be called from code that's actually written in class B (as opposed to code that class B gets by extending class A).
YES, you can implement the methodA method in class B, but, pay attention, you are not overriding it.
Your method is declared ad private so is not "visible" from extending classes.
If your intention is to make your method overridable, you need to declare it as public.
Just give it a try :)
public class Main {
public static void main(String[] args) {
Base base;
base = new A();
System.out.println(base.doSth());
base = new B();
System.out.println(base.doSth());
}
}
abstract class Base {
public abstract String doSth();
}
class A extends Base {
#Override
public String doSth() {
return "A";
}
}
class B extends A {
#Override
public String doSth() {
return "B";
}
}
I think you wonna override the super-class method, and to do this, the method on sub-class must have the same signature of super-class method.
You can call these methods in following ways:
Suppose test1 is an instance of classA, teste1.methodA(index) will execute the implementation on super-class.
Suppose test2 is an instance of classB, test2.methodA(index) will execute the sub-class method.
In classB you can invoque the super class method (if the method is notprivate), something like :
public class ClassB extends ClassA
{
...
super.methodA(index);
...
}
I Have this class:
public abstract class Test {
public abstract class SubClass extends Test
{
}
}
I need to access it like this
public class JavaApplication1 extends Test.SubClass {
public JavaApplication1()
{
super();
}
}
But having problem with super.
I Cant use it static nor extend Test
What should I do?
Thanks in advance
One solution: make SubClass a static inner class.
Another possible solution:
public class JavaApplication1 extends Test.SubClass {
public JavaApplication1() {
new Test() {}.super();
}
public static void main(String[] args) {
new JavaApplication1();
}
}
abstract class Test {
public Test() {
System.out.println("from Test");
}
public abstract class SubClass extends Test {
public SubClass() {
System.out.println("from SubClass");
}
}
}
Which returns:
from Test
from Test
from SubClass
Why are you extending an inner class from the outside of the class containing it? Inner class should be used only when you need a specific need inside a Class. If there are more classes that could benefit from the services offered by a Class then it shouldn't be inner but a top level class.
The problem is accessing an inner class needs an instance of the outer class. So, you can't directly invoke super() from your JavaApplication1 constructor. The closest you can get is by creating a 1-arg constructor, passing the instance of the outer class, and then invoke super on that instance: -
public JavaApplication1(Test test)
{
test.super();
}
And then invoke the constructor as: -
new JavaApplication1(new Test() {});
This will work fine. But, with 0-arg constructor, you would have to create an instance of Test class first (in this case, an anonymous inner class, since Test is abstract), and then invoke super().
public JavaApplication1() {
new Test() {}.super();
}
How do I create an object of an abstract class and interface? I know we can't instantiate an object of an abstract class directly.
You can not instantiate an abstract class or an interface - you can instantiate one of their subclasses/implementers.
Examples of such a thing are typical in the use of Java Collections.
List<String> stringList = new ArrayList<String>();
You are using the interface type List<T> as the type, but the instance itself is an ArrayList<T>.
To create object of an abstract class just use new just like creating objects of other non abstract classes with just one small difference, as follows:
package com.my.test;
public abstract class MyAbstractClass {
private String name;
public MyAbstractClass(String name)
{
this.name = name;
}
public String getName(){
return this.name;
}
}
package com.my.test;
public class MyTestClass {
public static void main(String [] args)
{
MyAbstractClass ABC = new MyAbstractClass("name") {
};
System.out.println(ABC.getName());
}
}
In the same way You can create an object of interface type, just as follows:
package com.my.test;
public interface MyInterface {
void doSome();
public abstract void go();
}
package com.my.test;
public class MyTestClass {
public static void main(String [] args)
{
MyInterface myInterface = new MyInterface() {
#Override
public void go() {
System.out.println("Go ...");
}
#Override
public void doSome() {
System.out.println("Do ...");
}
};
myInterface.doSome();
myInterface.go();
}
}
There are two ways you can achieve this.
1) Either you extend / implement the Abstract class / interface in a new class, create the object of this new class and then use this object as per your need.
2) The Compiler allows you to create anonymous objects of the interfaces in your code.
For eg. ( new Runnable() { ... } );
Hope this helps.
Regards,
Mahendra Liya.
You can provide an implementation as an anonymous class:
new SomeInterface() {
public void foo(){
// an implementation of an interface method
}
};
Likewise, an anonymous class can extend a parent class instead of implementing an interface (but it can't do both).
public abstract class Foo { public abstract void foo(); }
public interface Bar { public void bar(); }
public class Winner extends Foo implements Bar {
#Override public void foo() { }
#Override public void bar() { }
}
new Winner(); // OK
"instantiate" means "create an object of".
So you can't create one directly.
The purpose of interfaces and abstract classes is to describe the behaviour of some concrete class that implements the interface or extends the abstract class.
A class that implements an interface can be used by other code that only knows about the interface, which helps you to separate responsibilities, and be clear about what you want from the object. (The calling code will only know that the object can do anything specified in the interface; it will not know about any other methods it has.)
If you are using someone else's code that expects a Fooable (where that is the name of some interface), you are not really being asked for an object of some Fooable class (because there isn't really such a class). You are only being asked for an instance of some class that implements Fooable, i.e. which declares that it can do all the things in that interface. In short, something that "can be Foo'd".
You write a class that derives from the abstract class or implements the interface, and then instantiate that.
What you know is correct. You cannot create an object of abstract class or interface since they are incomplete class (interface is not even considered as a class.)
What you can do is to implement a subclass of abstract class which, of course, must not be abstract. For interface, you must create a class which implement the interface and implement bodies of interface methods.
Here are orginal tutorial on oracle site, http://download.oracle.com/javase/tutorial/java/IandI/abstract.html and http://download.oracle.com/javase/tutorial/java/concepts/interface.html
You can not instantiate the abstract class or an interface, but you can instantiate one of their subclasses/implementers.
You can't instantiate an abstract class or an interface, you can only instantiate one of their derived classes.
In your example
MyAbstractClass ABC = new MyAbstractClass("name") {
};
You are instantiating any class that implements Suprising.
public abstract class AbstractClass { ... }
public interface InterfaceClass { ... }
// This is the concrete class that extends the abstract class above and
// implements the interface above. You will have to make sure that you implement
// any abstract methods from the AbstractClass and implement all method definitions
// from the InterfaceClass
public class Foo extends AbstractClass implements InterfaceClass { ... }
NO, we can't create object out of an interface or Abstract class because
Main intention of creating an object is to utilize the wrapped methods and data.
As interface don't have any concrete implementation hence we cannot.
For abstract class we may have concrete method or abstract method or both.
There is no way for the API developer to restrict the use of the method thats don't have implementation.
Hope help.
No, you are not creating the instance of your abstract class here. Rather you are creating an instance of an anonymous subclass of your abstract class. And then you are invoking the method on your abstract class reference pointing to subclass object.