Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract  RSS feed

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public abstract class B {
public void print(){
System.out.println("Print method");
}
public void test() {
System.out.println("Hello");
}
public static void main(String args[]){
//B a = new B(); abstract class cann't be instantiated
//print(); cann't call a non-static method in a static method.
}
}
class try extends B {
//
//some code
}
how the methods test() and print() can be instantiated.
Please explain, abstract is a bit confusing for me.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You can not instantiate a method. May be you are asking "how to call the methods print() and test() ?"
2. Yes, you can not instantiate an abstract class.
3. Yes, you can not call a non-static method from a static method. But you can call a non-static method from a static method, if you have an instance of the class and qualify the method like "instance.method()". Look at the following example.
4. The following will print "Print Method" followed by "Hello"

/* B.java */
public abstract class B {
public void print(){
System.out.println("Print method");
}
public void test() {
System.out.println("Hello");
}
public static void main(String args[]){
Try try1=new Try();
try1.print();
try1.test();
}
}
class Try extends B {
}


[This message has been edited by vasansrini (edited August 06, 2000).]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The purpose of abstract class in not allowing the other classes to instantiate it. Generally u declare a class abstract if atleast one of the methods in u'r class has no implementation there by leaving those skeleton methods to be implemented by the classes to extend the abstract class. either the extending class has to implement all the non implemented skeleton methods of abstract class or otherwise the extending class itsekf has to be declared absrtact. Offcourse u can declare a class abstract even if all its methods has got some implementation. In that case such declaration only serves the purpose that it cannot be instantiated in other classes.
one way to use the methods methods test() and print(), in in query is by doing as follows.
public class try extends B {
//
//some code
public static void main(String args[])
{
try t1 = new try();
t1.test();
t1.print();
}
}
If u are including both classes(try & B) in the same java file, u have to remove the public word infront of class B as we can have atmost 1 top level public class per java file. and main method does not make any sense in the abstract class.
As I just joined the forum, i appreciate any corrections in my posting so that i do not repeate them in future postings.
hope it helps.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. The purpose of an abstract class is to have all the common behaviour in the abstract class and let the subclasses implement the specific behaviour.
"It is to set a pattern that can be filled out with concrete methods for a specific situation - Bill Brogden"
2. If you want no other class to instantiate a particular class, you could make all the constructors of that class private.
3. main method in an abstract class makes sense. please look at my earlier example

[This message has been edited by vasansrini (edited August 06, 2000).]
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!