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?
Related
I have the below two classes. I'm wondering how I'm able to call the instance method of ClassA i.e. AMessage() in class B with out the instance of a ClassA or ClassB created?
I was thinking I should call instance method of ClassA i.e. AMessage() in class B as below:
new ClassA().AMessage(); //no compile error
new ClassB().Amessage(); //no compile error
Parent Class (ClassA.java)
public class ClassA {
public void AMessage(){
System.out.println("A Message");
}
}
Child Class (ClassB.java)
public class ClassB extends ClassA{
public void BMessage(){
AMessage(); //no compile error
}
public static void main(String[] args){
new ClassB().BMessage();
}
}
When a class extends another class, it automatically inherits the visible fields and methods of the base class. By visible I mean accessible members. Private members are not inherited. Learn more about inheritance in http://www.tutorialspoint.com/java/java_inheritance.htm
So if you have a class ClassA having a method AMessage like this :
public class ClassA{
public void AMessage(){
System.out.println("A message");
}
}
and ClassB which extends ClassA like this :
public class ClassB extends ClassA{
public void BMessage(){
AMessage();
}
public static void main(String[] args){
new ClassB().BMessage();
}
}
ClassB automatically inherits members of ClassA i.e. they act as if they are members of ClassB itself. That's why we can call the instance method of ClassA inside ClassB without any instance, because they belong to ClassB as well. Also besides instance methods, you can call static methods like that as well. (But of course you can not call instance methods inside static methods.)
As an additional answer I would like to suggest that (although it is not related to the question but it is a good practice) you should not name any method of a class (instance or static) starting with a capital letter. It doesn't generate any compiler error if you still name it like that, but it affects the readability. I have written the names like that only to relate to your question.
Use super keyword to call base class methods. Check this for reference -
http://docs.oracle.com/javase/tutorial/java/IandI/super.html
public class ChildClass extends BaseClass{
public static void main(String[] args){
ChildClass obj = new ChildClass();
obj.Message();
}
public void Message(){
super.Message();
}
}
class BaseClass {
public void Message(){
System.out.println("Base Class called");
}
}
Or you can do it like this as well -
public class ChildClass extends BaseClass{
public static void main(String[] args){
new ChildClass().Message();
}
}
class BaseClass {
public void Message(){
System.out.println("Base Class called");
}
}
yes you can call method directly instance method of parent class from child class object provided parent class method should be public
you can call in this way
new ClassB().AMessage();
it will also work
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
}
}
I have 3 classes. It seems basic question. But I can'nt find answer by googling.
public abstract class Test {
void t1()
{
System.out.println("super");
}
}
public class concret extends Test{
void t1()
{
System.out.println("child");
}
void t2()
{
System.out.println("child2");
}
}
public class run {
public static void main(String[] args) {
Test t=new concret();
t.t1();
}
}
How do I call abstract class t1 method? Since I cant create object from abstract class how do I call t1 in abstract class?
Thank you.
Either you create a concrete class which doesn't override the method, or within a concrete class which does override the method, you can call super.t1(). For example:
void t1()
{
super.t1(); // First call the superclass implementation
System.out.println("child");
}
If you've only got an instance of an object which overrides a method, you cannot call the original method from "outside" the class, because that would break encapsulation... the purpose of overriding is to replace the behaviour of the original method.
you should be able to do it using
Test test = new Test(){};
test.t1();
Abstract class means the class has the abstract modifier before the class keyword. This means you can declare abstract methods, which are only implemented in the concrete classes.
For example :
public abstract class Test {
public abstract void foo();
}
public class Concrete extends Test {
public void foo() {
System.out.println("hey");
}
}
See following tests:
public abstract class BaseClass {
public void doStuff() {
System.out.println("Called BaseClass Do Stuff");
}
public abstract void doAbstractStuff();
}
public class ConcreteClassOne extends BaseClass{
#Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassOne Do Stuff");
}
}
public class ConcreteClassTwo extends BaseClass{
#Override
public void doStuff() {
System.out.println("Overriding BaseClass Do Stuff");
}
#Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassTwo Do Stuff");
}
}
public class ConcreteClassThree extends BaseClass{
#Override
public void doStuff() {
super.doStuff();
System.out.println("-Overriding BaseClass Do Stuff");
}
#Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassThree Do Stuff");
}
}
public class Test {
public static void main(String[] args) {
BaseClass a = new ConcreteClassOne();
a.doStuff(); //Called BaseClass Do Stuff
a.doAbstractStuff(); //Called ConcreteClassOne Do Stuff
BaseClass b = new ConcreteClassTwo();
b.doStuff(); //Overriding BaseClass Do Stuff
b.doAbstractStuff(); //Called ConcreteClassTwo Do Stuff
BaseClass c = new ConcreteClassThree();
c.doStuff(); //Called BaseClass Do Stuff
//-Overriding BaseClass Do Stuff
c.doAbstractStuff(); //Called ConcreteClassThree Do Stuff
}
}
use keyword 'super' to do that
void t1()
{ super.t1();
System.out.println("child");
}
Make sure you use that in the overriden method though.
Your code seems to call t1(). However this is calling the concrete t1() because the abstract t1() has been overridden by the concrete class.
If you wish to call the abstract t1 method from main code, do not override the t1() in concrete.
Or you can create a method in the concrete class for example:
public void invokeSuperT1(){
super.t1();
}
Create an anonymous Inner class,
Abstract class:
abstract class Test{
abstract void t();
public void t1(){
System.out.println("Test");
}
}
Here is how to create anonymous inner class:
Test test = new Test() {
#Override
void t() {
//you can throw exception here, if you want
}
};
Call the class via the object created for abstract class,
test.t1();
An abstract class is used when we want that every class that inherited from our abstract class should implement that abstract method, so it is must to implement method otherwise it gives the compile-time error.
void t1()
{
super.t1; // means the parent methods
System.out.println("child");
}
For example: Bird class has method sing() and there other classes that inherited from it like the sparrow, Pigeon, Duck, so these all have sing method so we make Bird class Abstract and make the sing() method abstract in it so every child of bird that implements Bird class should have a method of sing() with its on implementation.
First Create abstarct class like as shown in link: Creating Abstract Class
Create Sub-Classs like as shown in link: Sub-class extending
Creating main method for executing this as show in link: Instanciate the subclass to access
Result as shown here: Result
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
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