Pin It

How Inheritance works in Java through extends keyword ?

Program to demonstrate how Inheritance works in Java through extends keyword.

 class Shape {
    private String s = " Shape: ";
 
    public void draw(){
 name(" draw() ");
    }
    public void redraw(){
 name(" redraw() ");
 }
    public void area(){
 name(" area() ");
    }
    public void circumference(){
 name(" circumference() ");
    }
    public void name(String a){
 s+=a;
    }
    public String toString() {
 return s;
    }
 
    public static void main(String[] args) {
  
    Shape s = new Shape();
    s.draw();
    s.redraw();
    s.area();
    s.circumference();
    System.out.println(s);

    }

}

 
public class Circle extends Shape{

    public void area(){
 name(" Circle.area() ");
    }
    public void circumference(){
 name(" Circle.circumference() ");
    }
    
    public static void main(String[] args) {
  
 Circle c = new Circle();
    c.draw();
    c.redraw();
    c.area();
    c.circumference();

    System.out.println(c);
    Shape.main(args);


 }

}




Output of the program :



Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot