abstract class Shape{ abstract void draw(); } class Rectangle extends Shape{ void draw(){ System.out.println("Drawing Rectangle"); } } class Traingle extends Shape{ void draw(){ System.out.println("Drawing Traingle"); } } class AbstractDemo{ public static void main(String args[]){ Shape s1 = new Rectangle(); s1.draw(); s1 = new Traingle(); s1.draw(); } }
public class Car { int price; String name; String color; int engineCapacity; int engineHorsePower; String wheelName; int wheelPrice; void move(){ //move forward } void rotate(){ //Wheels method } void internalCombustion(){ //Engine Method } }
public class Car { Engine engine = new Engine(); Wheel wheel = new Wheel(); int price; String name; String color; void move(){ //move forward } }
public class Engine { int engineCapacity; int engineHorsePower; void internalCombustion(){ //Engine Method } }
public class Wheel { String wheelName; int wheelPrice; void rotate(){ //Wheels method } }
abstract class clsName { // Variable declaration // Constructor // Method abstract rtype mthName(params); }
abstract class Shape { public static float pi = 3.142; protected float height; protected float width; abstract float area(); } class Square extends Shape { Square(float h, float w) { height = h; width = w; } float area() { return height * width; } } class Rectangle extends Shape { Rectangle(float h, float w) { height = h; width = w; } float area() { return height * width; } } class Circle extends Shape { float radius; Circle(float r) { radius = r; } float area() { return Shape.pi * radius *radius; } } class AbstractMethodDemo { public static void main(String args[]) { Square sObj = new Square(5,5); Rectangle rObj = new Rectangle(5,7); Circle cObj = new Circle(2); System.out.println("Area of square : " + sObj.area()); System.out.println("Area of rectangle : " + rObj.area()); System.out.println("Area of circle : " + cObj.area()); } }
Labels: Core JAVA