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
- React Native
- 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
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)