• 0

[JAVA] Abstract Class Concept


Question

A teacher gave me this lecture with some questions, and I am somewhat confused.

Say we have a hierarchy like:

Object

|

+---A

|

+---B

|

+---C

and we made these methods in Class A:

public abstract void method1();

public abstract void method2(int i);

so... these questions were presented to us:

(a) If class B does not override any of the two methods above, can class B be compiled cleanly? If so, can an object be created from class B? If not, explain why not.

(b) Now assume that class B overrides method1 given above. Can class B be compiled cleanly? If so, can an object be created from class B? If not, explain why not.

© Now assume that class C overrides method2 given above (with the assumption that B overrides method1, as discussed in part(b)). Can class C be compiled cleanly? If so, can an object be created from class C? If not, explain why not.

(d) In defining class A described above, must the abstract modifier be used in the 1st line of the class definition (or is it optional given that it has abstract method(s) in it)?

(e) In defining method1 and method2 described above, must the abstract modifier be used in the method statement (or is it optional given that it has no method body)?

Ok, so if a subclass, which would be B, doesn't override the abstract methods, it becomes an abstract class, correct? But I'm not really sure if it can create an object or not, I don't believe it can, but can someone at least elaborate on this.

Any help would be appreciated.

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

same as he said.

"Abstract" tell classes that inherit from it they it must implement these abstract methods.

Abstract class and methods can not be created as they have no implementation. They are classes designed to be inherited by a subclass

eg.

class Shape 
{
   abstract void drawMe()
  {
     //sub classes should implement this
     //no code here
  }
}

class Triangle extends Shape
{
   void drawMe()
  {
    // draw a triangle here
    //put your code here
   }
}

class Square extends Shape
{
   void drawMe()
  {
    // draw a square here
    // put your code here
   }
}

so now as long as we know the object is instanceof object we can call the draw method

somefunction(Shape x)
{
   //since we call drawMe() cause we know all subclasses of shape implement this method
   x.drawMe();
}

someFunction2(Object x)
{

  // we can cast if object is instance of shape and call drawMe() method
  if (x instanceof Shape)
    (x(Shape)).drawMe();
}

main
{
   Triangle t = new Triangle();
   Square s = new Square();


   //we can now call these functions with these objects
   somefunction(t);
   somefunction(s);
   
   somefunction2(t);
   somefunction2(s);
}

Link to comment
Share on other sites

  • 0

(a) If class B does not override any of the two methods above, can class B be compiled cleanly? If so, can an object be created from class B? If not, explain why not.

(b) Now assume that class B overrides method1 given above. Can class B be compiled cleanly? If so, can an object be created from class B? If not, explain why not.

? Now assume that class C overrides method2 given above (with the assumption that B overrides method1, as discussed in part(b)). Can class C be compiled cleanly? If so, can an object be created from class C? If not, explain why not.

(d) In defining class A described above, must the abstract modifier be used in the 1st line of the class definition (or is it optional given that it has abstract method(s) in it)?

(e) In defining method1 and method2 described above, must the abstract modifier be used in the method statement (or is it optional given that it has no method body)?

Answers

a) No, it cannot it must provide implementation for both methods, so it MUST override the two methods

b) It still cannot be, because the implementation for the other abstract method isn't overriden.

c) Yes, class C can be compiled, because Class B overrides one method and essentially it's inherited that abstract method as well, and Class C inherited the overriden method and and also provides an implementation for that other method.

d) No, it doesn't have to be used, providing there is at least one or more abstract methods, that class becomes an abstract class as opposed to an concrete class

e) No, it must be declared abstract to force the sub-class to override the method, otherwise it's a simple method that has emtpy implementation and when invoked will do nothing.

Link to comment
Share on other sites

  • 0
a) No, it cannot it must provide implementation for both methods, so it MUST override the two methods

I don't think this is right. You can have a chain of abstract classes, but you cannot create an object of an abstract class. Once all abstract methods have been overridden, you can create objects.

Link to comment
Share on other sites

  • 0

hmm, I personally don't think you should give direct answers to questions given to him/her as a tutorial assignment. It's important that he has a try at the question

Link to comment
Share on other sites

  • 0

Actually, all I wanted was some explanation of abstract classes, but I got some of it before you guys answered the questions, but thanks for the input, I really feel like I understand abstract classes better than the lecture and notes that were given.

Link to comment
Share on other sites

  • 0
I don't think this is right. You can have a chain of abstract classes, but you cannot  create an object of an abstract class. Once all abstract methods have been overridden, you can create objects.

584774123[/snapback]

Well yeah exactly, the question asked if B only overrided one of the abstract methods can B still be instantiated, it won't allow it, because it inherits the abstract class and is thus obliged to implement all abstract methods, or else it will whinge.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.