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



Ask a Question

Subscribe to Tutorials

Related Tutorials

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

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. realy i enjoy it for visit your site. and i unders
View Tutorial          By: vinod balot at 2015-02-09 17:43:30

2. It was sooo helpful for me
Thanx and keep i

View Tutorial          By: Shan at 2010-04-13 09:41:01

3. good resource
View Tutorial          By: AJITH C K at 2013-04-10 17:45:51

4. This is helpful for newbies like me :P
View Tutorial          By: Mustafa at 2011-06-18 23:40:27

5. hi,this is nishad from india.....
i have a

View Tutorial          By: NISHAD at 2008-09-06 09:23:15

6. Hello all,Actually i want xml parser in blackberry
View Tutorial          By: arun at 2011-06-01 06:17:16

7. The code to show recursion is ok, but the declara
View Tutorial          By: Gaurav at 2010-08-08 01:08:27

8. Good examples. However... I am testing for a Hex v
View Tutorial          By: Doug Whitehead at 2008-11-29 17:10:43

9. hai sir... this is ROSHAN I am searching for voice
View Tutorial          By: roshan at 2013-09-25 08:12:55

10. Where should I put the xml file ??????????
View Tutorial          By: Marya at 2014-03-21 16:01:13