avoid inheritence without using final keyword

i want to stop inheritence of a class without using keyword final sugg

Page 1 of 1

3 Replies - 1852 Views - Last Post: 06 May 2009 - 03:13 PM Rate Topic: -----

#1 sonirameshr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-November 08

avoid inheritence without using final keyword

Post icon  Posted 05 May 2009 - 11:57 PM

i want to stop inheritence of a class without using keyword final suggest me some solution.
for example

final class Animal{
}
class Dog extends Animal{
}

the above thing is not possible bcoz of keyword final , but can you make this thing happen without keyword final.
Is This A Good Question/Topic? 0
  • +

Replies To: avoid inheritence without using final keyword

#2 avinashsaniva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-August 06

Re: avoid inheritence without using final keyword

Posted 06 May 2009 - 05:00 AM

class Animal {
	private Animal() {}
}

class Dog extends Animal {
}



Again the above thing is not possible. There is no use of final keyword.

This post has been edited by avinashsaniva: 06 May 2009 - 05:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1935
  • View blog
  • Posts: 4,018
  • Joined: 11-December 07

Re: avoid inheritence without using final keyword

Posted 06 May 2009 - 05:53 AM

class Animal {
    private Animal() {}
    public static Animal makeAnAnimal() {return new Animal();}
}

class Dog extends Animal {
}


That would do it and still give you the functionality of the constructor. Why can't you use the keyword final?

This post has been edited by cfoley: 06 May 2009 - 05:54 AM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: avoid inheritence without using final keyword

Posted 06 May 2009 - 03:13 PM

That is why the final keyword was invented.
Why do you want to avoid it ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1