Comment on Tutorial - Abstract classes in Java By Kamini
Comment Added by : Vikash K Agarwal
Comment Added at : 2011-11-02 12:02:50
Comment on Tutorial : Abstract classes in Java By Kamini
Here i posted a good example with spring. Here really implementation of OOPS.
public abstract class Shape {
public abstract double getArea();
public void printInfo()
{
System.out.printf("%s with area of %,.2f%n",getClass().getSimpleName(), getArea());
}
public class Circle extends Shape {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI*getRadius()*getRadius();
}
public Circle(double radius)
{
setRadius(radius);
}
}
public class Rectangle extends Shape {
private double length,width;
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public double getArea() {
return getLength()*getWidth();
}
public Rectangle(double length,double width)
{
setLength(length);
setWidth(width);
}
public Rectangle(){}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ShapeTest {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
Shape shape=(Shape)applicationContext.getBean("Rectangle");
shape.printInfo();
Rectangle rect=(Rectangle)shape;
rect.setLength(15.0);
rect.setWidth(10.0);
Shape shape2=(Shape)applicationContext.getBean("Rectangle");
shape2.printInfo();
Shape shape1=(Shape)applicationContext.getBean("Circle");
shape1.printInfo();
}
}
View Tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
Archived Comments
1. nice...cleared this example....
View Tutorial By: Waqas Mughal at 2010-03-13 21:21:46
2. Hi
I tried to use this class , I don't get
View Tutorial By: arnav at 2015-03-27 06:01:22
3. thanks
View Tutorial By: sowmya at 2012-07-10 14:22:50
4. really helpul........
pls mail me some more
View Tutorial By: shivani at 2010-07-17 01:47:30
5. thanks! it helped!
View Tutorial By: shgy at 2015-03-17 07:42:40
6. Hello
thanks for nice tutorial. I have one
View Tutorial By: Chanchal Prajapat at 2015-02-09 06:10:42
7. Buenisimo, me ayuda a salir de un problema, cosas
View Tutorial By: Emilio, Guatemala at 2012-09-25 15:33:11
8. I managed to run the code but I'm getting a *** ti
View Tutorial By: Bull at 2012-01-11 12:26:57
9. really a gud work for xml parsing readers
View Tutorial By: shreya at 2010-07-02 02:35:04
10. if the "extend" word is not present does
View Tutorial By: detteskii at 2011-06-27 09:12:23