You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

In contrast to Python, Java supports only object-oriented programming, while Python supports many (but not all) aspects of object-oriented programming; but it is possible to write a Python program without making any use of OO concepts.

Strings in Java
class Animal{
	private String name;
	public Animal(String name){
		this.name = name;
	}
	public void saySomething(){
		System.out.println("I am " + name);
	}
}
 
class Dog extends Animal{
	public Dog(String name) {
		super(name);
	}	
	public void saySomething(){
		System.out.println("I can bark");
	}
}
 
public class Main {
	public static void main(String[] args) {
		Dog dog = new Dog("Chiwawa");
		dog.saySomething();
 
	}
}
Strings in Python
class Animal():
 
        def __init__(self, name):
            self.name = name
 
        def saySomething(self):
            print "I am " + self.name    
 
class Dog(Animal):
        def saySomething(self):
            print "I am "+ self.name \
            + ", and I can bark"
 
dog = Dog("Chiwawa") 
dog.saySomething()
  • No labels