Please explain all these four pages-
Open the images in paint. Only then will you be able to view it.

Thanks in advance.

Recommended Answers

All 13 Replies

it is all explained on those four pages, that's what the book is for. what exactly don't you understand, and what do you think it means?

my doubts are --

Biggest doubt ever in Java -- 1.why do we need abstract methods?
(I know, it is written in the "There are no dumb questions", but I don't get them. Why do I need abstract methods for polymorphism?
2.what is "implementation"?

1. can you do without them, sure ...
can you write polymorphic code without them .. sure.
let's say you have a class car.
and you want to divide that in (say:) a sportscar, a familycar, ...
all of these will have different implementations of:
shiftUp() or speedUp()
since that all depends on the engine. but they are all cars.
so, you could write a normal car class, but since there is no 'default' speedUp..

you can also make it abstract.

an abstract class is a bit like an interface. it tells you WHICH methods to implement, not HOW to implement them.
but, you can see every object as
Car b = new SportsCar();...
so you can store them in an ArrayList<Car>

2. you have inheritance (Class A extends B)
and you have implementing of classes, like interfaces
inheritance gives you complete code which you can reuse
implementation tells you: (at least) these methods should be implemented in your class, even if it's just a mock method.

Couldn't that be done by overriding methods?

public class Car {
public void speedUp(int speed) {
    int newSpeed = speed + [B]1[/B];
public class SportsCar extends Car {//I am going to override the normal speedUp() method

public void speedUp(int speed) {
    int newSpeed = speed + [B]2[/B];

I'll try to explain this with an example. Let's talk about a simple templating system which is based on a single Template interface. Every different template implementation has a different way of interpolating variables, convention used for representing data etc. But all templates also share a common piece of logic: they all interact with the "environment" i.e. state, they all are capable of being serialized to common formats like PDF, PS etc.

My concern as a template framework provider would be to prevent code/effort duplication for template implementers i.e. classes which provide different template support. To that end, I create a convinience base class which would be extended by all template implementers and which has the common logic required for all templates. But I have two problems:

  1. I can't enforce template writers to override/implement a method. For e.g. I would want each template writer to override the render() method which takes a template, environment and produces a rendered output as String.
  2. I can't prevent instantiation of the BaseTemplate class.

These two problems can't be easily handed by a normal class. Enter abstract classes. You want to enforce template implementers to override a given method? No problem, just make it abstract. You want to enforce non-instantiation? No problem, make the entire class abstract.

Of course, I'll also mention that you can't have abstract methods without making the entire class abstract because as per common sense, you'll really not want to "instantiate" something which isn't complete (yes, abstract classes can be thought of as "imcomplete" classes).

commented: never been good at explaining myself :) +12

Sorry, but I still didn't get it.

Sorry, but I still didn't get it.

Fair enough. To understand the need for a solution, you need to first have a good grasp on the problem you are trying to solve. So my question to you is: what would be your solution if you are asked to come up with a way to ensure that clients are forced to implement a subset of methods found in an interface? E.g.

interface Template {
  Environment getEnvironment();
  byte[] renderToImage();
  String renderToString();

Let's say renderToImage() is a convenience method which uses renderToString() to output the image. If I have to create a class which only needs to implement renderToString(), what would be your solution?

Check here the java docs help me understand even the most complex of definitions: another good def:

Please don't quote Roseindia, they are content thieves.

Please don't quote Roseindia, they are content thieves.

Lol :P, oh sorry never knew.

So I can come to the conclusion that :-

  1. Abstract class cannot be instantiated.
  2. Abstract methods HAVE to be overridden.

Thanks a LOT, all(and I mean ALL with capital A, L and L) of you guys who responded.

Also explain(if you can) :-

  1. What's the difference between an abstract class which has all it's methods marked abstract and an interface?

An abstract class can declare ordinary variables. An interface can only declare final static "variables" (AKA constants)

final static "variables" (AKA constants)

Umm.. actually I am teaching all this stuff to a ten year old.So he's not learnt final static yet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.