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



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

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

Archived Comments

1. The SAX Parser code has a problem for files of gre
View Tutorial          By: Mark Nelson at 2011-11-14 20:16:41

2. I am an MCA student i wanna know whether one can s
View Tutorial          By: RAJU.K at 2009-07-24 02:28:21

3. Hi Ramlak,

I tried to use the datab

View Tutorial          By: Braj at 2009-02-07 03:31:13

4. Really superb.Simple code.Thanks.
View Tutorial          By: pradesh at 2010-04-30 01:09:57

5. THIS IS VERY SIMPLE AND EXCELENT PROGRAM.THIS PROG
View Tutorial          By: CHIRANJEET at 2011-02-05 12:13:54

6. Finally a tutorial that clearly explains why Hasht
View Tutorial          By: Lisa Ander at 2011-10-27 20:33:15

7. Hibernate can be an effective framework if one app
View Tutorial          By: Giao Vu at 2011-08-03 23:40:02

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

9. Daniel Jonsson, you should be thankful to this art
View Tutorial          By: dole at 2011-11-12 06:44:42

10. How to delete file button
View Tutorial          By: Chinzoo at 2013-04-13 13:30:18