Intro of JAVA
Learn Java
Syntax:
class subclass-nameextendSuperclass-name{
//methods and fields
}
Example
class Birds
{void fly() {System.out.println("Flying...");}
}
class Zoo extends Birds
//So here we have extend the Zoo class into the Bird class
{
void chirp() {System.out.println("Chirping...");}
}
class Saurabh{
public static void main (String args[])
{
Zoo z=new Zoo();// here we have created the object of Zoo class
z.chirp();// here we are calling the method of Zoo class
z.fly();// here we are calling the method of Bird class
}
}
Output:
Example
class Birds
{
void fly() {System.out.println("Flying...");}
}
class Pigeon extends Birds
// so here we are extending the Pigeon class into Bird class
{
void eat() {System.out.println("Eating...");}
}
class Zoo extends Pigeon
//So here we are extending the Zoo class into the Pigeon class
{
void chirp() {System.out.println("Chirping...");}
}
class Saurabh
{
public static void main (String args[])
{
Zoo z=new Zoo();// here we have created the object of Zoo class
z.chirp();// here we are calling the method of Zoo class
z.fly();// here we are calling the method of Birds class
z.eat();// here we are calling the method of Pigeon class
}
}
Output:
Example
class Birds
{
void fly() {System.out.println("Flying...");}
}
class Pigeon extends Birds
// so here we are extending the Pigeon class into Bird class
{
void eat() {System.out.println("Eating...");}
}
class Zoo extends Birds
//So here we are extending the Zoo class into the Pigeon class
{
void chirp() {System.out.println("Chirping...");}
}
class Saurabh
{
public static void main (String args[])
{
Zoo z=new Zoo();// here we have created the object of Zoo class
z.chirp();// here we are calling the method of Zoo class
z.fly();// here we are calling the method of Birds class
/*z.eat();// compile run time error because our Zoo class is been derived from Bird class not from the Pigeon class */
}
};
Output:
Example
class Birds {
void fly() {System.out.println("Flying...");}
}class Pigeon // So here we have taken new class Pigeon
{
void eat() {System.out.println("Eating...");}
}
class Zoo extends Birds ,Pigeon; // So here we are extending both Birds and Pigeon
class
{
void chirp() {System.out.println("Chirping...");}
}
class Saurabh
{
public static void main (String args[])
{
Zoo z=new Zoo ();// here we have created the object of Zoo class
z.chirp();// we are calling here the method of Zoo class
z.fly();// here we are calling the method of Birds class
}
Output:
Compile time error
Example
class Birds
{
void fly() {System.out.println("Flying...");}
}
class Peacock extends Birds
// so here we are extending the Peacock class into Bird class
{
void dance() {System.out.println("Dance...");}
}
class Pigeon extends Peacock
//So here we are extending the Pigeon class into the Peacock class
{
void eat() {System.out.println("Eating...");}
}
class Koyal extends Pigeon
// So here we are extending the Koyal class into the Peacock class
{
void sing() {System.out.println("Singing...");}
}
class Saurabh
{
public static void main (String args[])
{
Koyal k=new Koyal();// here we have created the object of Zoo class
k.sing();// here we are calling the method of Zoo class
k.eat(); // here we are calling the method of Birds class
k.dance();
k.fly();
}
};
Output: