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. Good
View Tutorial          By: Siva Sreekanth at 2013-10-01 16:29:55

2. i want full of information abt java
View Tutorial          By: sagar at 2010-08-23 06:42:40

3. Thank alot
View Tutorial          By: Naganatarajan at 2010-01-07 05:54:17

4. What is meant by source code portability?
View Tutorial          By: sathihs at 2012-11-02 11:16:57

5. Hi,
Its good but you can make it more bette

View Tutorial          By: vishakha sharma at 2010-12-15 06:14:52

6. thanks
View Tutorial          By: snooker at 2013-02-04 19:45:40

7. sample program exception handling
View Tutorial          By: priya at 2009-09-10 05:40:29

8. this code should be commented I mean a lot of comm
View Tutorial          By: mmmm at 2012-03-31 00:44:41

9. It worked. Thank U!
View Tutorial          By: Abhijit Kurane at 2010-12-27 11:01:48

10. HELP! HELP! HELP!
i got an time out at step

View Tutorial          By: sumit at 2009-04-03 20:19:51