Driver program for Abstract class - java

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
}
}

Related

creating Abstract class object java

public abstract class AbstractObject {
AbstractObject(){
System.out.println("mahi2");
}
}
class PrintMahi extends AbstractObject{
public static void main(String ... args){
PrintMahi m = new PrintMahi();
}
}
In above code I understand that the PrintMahi object is getting created and because of inheritance superclass which is abstract in this case constructor is getting called. So is it like that the abstract class object is being created here?

Calling a method of abstract class from a concrete class without extending

Example abstract class is bellow.
public abstract class Vehicle {
void maintain(String str) {
System.out.println(str);
}
}
Example concrete class is bellow.
public class Driver {
public static void main(String[] args) {
}
}
Now I need to access the maintain method without extending the Vehicle class.Is there any way to do this without using static content?
No, there isn't, because maintain is an instance method. To call an instance method, you must have an instance. You can't create an instance of an abstract class.
You can subclass it anonymously (see this tutorial), but you still need to subclass it.
You can use an anonymous inner class. I've used your example code but also defined an anstract method in Vehicle
public class AbstractTest {
public static void main(String[] args){
Vehicle v = new Vehicle() {
#Override
void myOtherAbstractMethod() {
// Do what you need here
}
};
v.maintain("foo");
}
public static abstract class Vehicle {
void maintain(String str) {
System.out.println(str);
}
abstract void myOtherAbstractMethod();
}
}
You cannot do that as abstract classes are abstract. Also in your case there's no connection between Driver and Vehicle so even if you would be able to compile that code (you won't), then ClassCastException would show up.
You must extend abstract class first, like it or not.

Abstract class in Java.

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

Using Sub Abstract Class in Java

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 to create an object of an abstract class and interface

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.

Categories

Resources