Question about Abstract class

not basic not hard either, probably very simple so it is basic or hard

Page 1 of 1

9 Replies - 2291 Views - Last Post: 03 May 2010 - 05:16 PM Rate Topic: -----

#1 obviousninja  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 17-February 10

Question about Abstract class

Posted 03 May 2010 - 10:17 AM

ok so i have a question regarding the abstract class. so i will assume we all know that classes implement the abstract class have to have implement the abstract methods inside the abstract class. the part which i don't understand is inside the abstract class, there are also some methods that are not abstract, and from my experience you can't use them to instantiate anything or use to them do anything, so what's the point of having them around? why do i see them around in other people's code? why not just make a class that is full of abstract method and instead of putting any variables or constructors down to that will do nothing. thanks for the clarification.

Is This A Good Question/Topic? 0
  • +

Replies To: Question about Abstract class

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Question about Abstract class

Posted 03 May 2010 - 10:28 AM

The whole point of an abstract class is for inheritance and polymorphism. Since you cannot instantiate an abstract class, you will have to extend it and implement the abstract methods. The subclass(es) also inherit the non-abstract components, so you do not have to redefine them for each subclass (though you can override them).

So for example, you are given an abstract class GeometricObject with abstract method getArea(). If you extend GeometricObject to get a Triangle class, Circle class, and Rectangle class, you would define getArea() differently for each of these classes as each shape has a different area formula.

So now, if you have a superclass pointer for a subclass Object, the functionality of getArea() will be determined at runtime by the type of GeometricObject being referenced. For example:
GeometricObject o = new Triangle(); 

//returns base * height/2 if that's how you define getArea() for Triangle
o.getArea(); 

GeometricObject b = new Rectangle();
b.getArea(); //can be different than getArea() for Triangle



For more information on Abstract Classes, check out japanir's tutorial on Abstract Classes vs. Interfaces.

This post has been edited by macosxnerd101: 03 May 2010 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

#3 obviousninja  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 17-February 10

Re: Question about Abstract class

Posted 03 May 2010 - 11:00 AM

View Postmacosxnerd101, on 03 May 2010 - 09:28 AM, said:

The whole point of an abstract class is for inheritance and polymorphism. Since you cannot instantiate an abstract class, you will have to extend it and implement the abstract methods. The subclass(es) also inherit the non-abstract components, so you do not have to redefine them for each subclass (though you can override them).

So for example, you are given an abstract class GeometricObject with abstract method getArea(). If you extend GeometricObject to get a Triangle class, Circle class, and Rectangle class, you would define getArea() differently for each of these classes as each shape has a different area formula.

So now, if you have a superclass pointer for a subclass Object, the functionality of getArea() will be determined at runtime by the type of GeometricObject being referenced. For example:
GeometricObject o = new Triangle(); 

//returns base * height/2 if that's how you define getArea() for Triangle
o.getArea(); 

GeometricObject b = new Rectangle();
b.getArea(); //can be different than getArea() for Triangle



For more information on Abstract Classes, check out japanir's tutorial on Abstract Classes vs. Interfaces.


that's not what i meant... what i am trying to find out is eh... why do one put anything that is not abstract inside the main abstract class knowing that the main abstract class will not be instantiatable. maybe i should have said this from the start XD. sorry for the confusion.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Question about Abstract class

Posted 03 May 2010 - 12:03 PM

For the purpose of inheritance. When you extend the Abstract class, the subclass inherits everything from the Abstract class, including the methods and attributes. So you do not have to re-implement the non-abstract methods from the abstract class.
Was This Post Helpful? 0
  • +
  • -

#5 obviousninja  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 17-February 10

Re: Question about Abstract class

Posted 03 May 2010 - 12:52 PM

View Postmacosxnerd101, on 03 May 2010 - 11:03 AM, said:

For the purpose of inheritance. When you extend the Abstract class, the subclass inherits everything from the Abstract class, including the methods and attributes. So you do not have to re-implement the non-abstract methods from the abstract class.


so does that mean if i have an abstract class called evilClown and in that class i have String called noBigDeal. In the class there is also a not abstract method called evilClownKill(). in its subclass called Poopickingface i also need both the String and the method i mentioned so far. you said "So you do not have to re-implement the non-abstract methods from the abstract class." so that means i don't need to have noBigDeal and evilClownKill() in the Poopickingface and i can still invoke them? sory lol my example is always strange for my purpose XD. i am kinda confused here.
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Question about Abstract class

Posted 03 May 2010 - 12:57 PM

I think that anyone reading that post and trying to help find your example quite confusing and not very clear..
could you post an example for what you mean in code? classes, methods etc..
Was This Post Helpful? 0
  • +
  • -

#7 stingray2772  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 12-February 06

Re: Question about Abstract class

Posted 03 May 2010 - 01:15 PM

It seems to me that you are getting stuck on the difference between an interface and an abstract class. An abstract class can define generic methods that CAN be overridden.

Say for example you have an abstract class called AbstractCup. In the abstract class a method named GetSize is defined which returns the private property size.

Now we have another class that implements AbstractCup, we'll call it SheetzMug. In the constructor we set size to 16 ounce. Now instead of providing a GetSize, it is already defined.

Thats a very basic example.

Now take it another step and look at it how I use this application in real life. I have an abstract class named AbstractTest. Abstract test has multiple abstract methods such as Run, BuildInstrumentList, CheckInstrumentList, and Reset. Each class implementing Abstract Test must define these methods. Regardless of the test, there are methods I don't need to override such as GetState, GetSpecifications, GetParameters.

Does that help at all?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Question about Abstract class

Posted 03 May 2010 - 02:22 PM

@obviousninja: Your explanation of your example wasn't really clear, so I'll provide a simple example to illustrate.

abstract class A{

    public int getOne(){return 1;}
    public int getTwo(){return 2;}
    public abstract int getX(int x);
}

class B extends A{

   /*getOne() and getTwo() are
     inherited from class A,
     so we do not need to redefine
     them as they already exist
     in class B. This allows us to 
     focus on the differences between B
     and A without rewriting all the similarities
   */

   public int getX(int x){return x;}
}


Was This Post Helpful? 1
  • +
  • -

#9 Guest_obviousninja*


Reputation:

Re: Question about Abstract class

Posted 03 May 2010 - 05:14 PM

View Postmacosxnerd101, on 03 May 2010 - 01:22 PM, said:

@obviousninja: Your explanation of your example wasn't really clear, so I'll provide a simple example to illustrate.

abstract class A{

    public int getOne(){return 1;}
    public int getTwo(){return 2;}
    public abstract int getX(int x);
}

class B extends A{

   /*getOne() and getTwo() are
     inherited from class A,
     so we do not need to redefine
     them as they already exist
     in class B. This allows us to 
     focus on the differences between B
     and A without rewriting all the similarities
   */

   public int getX(int x){return x;}
}



cool, that make sense. thanks for explaining, that just cleared things up for me. thanks guys.
Was This Post Helpful? 0

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Question about Abstract class

Posted 03 May 2010 - 05:16 PM

Not a problem. Glad we could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1