Question:
what is the need for abstract class in java?
2010-08-19 21:40:40 UTC
In java we use abstract class which contain abstract methods declaration and other concrete method definition.My question is why we need abstract class with out that also we can do program.So exactly what is the need for abstract class in java?
Three answers:
2010-08-23 19:03:34 UTC
well where we required multiple in inheritance or where function or operator over loading or over riding is there



We need the notion of a parent class (from which we can inherit) that is prohibited from being instantiated. We can think about abstract classes as semi complete generalized implementations that can not exist as objects on their own, they can only exist as parts of child objects.
Sivasankaranarayanan
2010-08-19 21:49:15 UTC
We need the notion of a parent class (from which we can inherit) that is prohibitted from being instantiated. We can think about abstract classes as semicomplete generalised implementations that can not exist as objects on their own, they can only exist as parts of child objects.

For example, think of a “Shape” hierarchy with subclasses such as Rectangle, Circle, Line etc. As shapes share data and implementation (i.e. coordinates, ability to paint themselves) it makes sense generalising, that is, creating a parent class (Shape) that contains all common aspects of the shapes. So at this point “Shape” is just a superclass.

It does not make sense however being able to instantiate an object of the class Shape because it misses critical information. To prohibit it, we make the class an abstract class.
?
2016-10-21 10:03:49 UTC
summary has no difficult approach definitions. you could't instantiate an summary classification, you may sub-classification it. // this is theory code, some greater issues could be further to run... summary classification Fish { summary void print(); } public classification FreshWater extends Fish { //overrides the summary approach, required public void print() { equipment.out.println("Freshwater"); } } public classification SaltWater extends Fish { public void print() { equipment.out.println("Saltwater"); } } public classification Salmon extends Freshwater {} Salmon sockeye = new Salmon(); sockeye.print(); // if i desire the great classification approach, rather of the subclass approach great.print(); // you may set up a heirarchy including this so which you've gotten an user-friendly array Fish[] fish = new Fish[3]; fish[0] = new Freshwater(); fish[a million] = new Saltwater(); fish[2] = new Freshwater();


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...