Inheritence
Java supports inheritance and thus, variables and methods of the superclass are inherited and can
be used by the subclass. but the private members of the superclass that cannot be accessed
directly from the subclass.
inheritenceexample.java
class Animal {//super class
public Animal() {
System.out.println("A new animal has been created!");
}
public void eat() {//super class methods eat and moves
System.out.println("An animal eats...");
}
public void moves() {
System.out.println("An animal movess...");
}
}
class Cow extends Animal {//Cow subclass
public Cow() {
super();//used to invoke super class constructer
System.out.println("A new cow has been created!");
}
@Override
public void eat() {
System.out.println("A Cow eats...");
}
@Override
public void moves() {
System.out.println("A Cow movess...");
}
}
class Dog extends Animal {//sub class Dog
public Dog() {
super();
System.out.println("A new dog has been created!");
}
@Override
public void eat() {
System.out.println("A dog eats...");
}
@Override
public void moves() {
System.out.println("A dog movess...");
}
}
public class inhertenceexample {//main class
public static void main(String[] args) {
Animal animal = new Animal();
Cow Cow = new Cow();
Dog dog = new Dog();
System.out.println();
animal.eat();
animal.moves();
Cow.eat();
Cow.moves();
dog.eat();
dog.moves();
}
}
output
A new animal has been created!
A new animal has been created!
A new cow has been created!
A new animal has been created!
A new dog has been created!
An animal eats...
An animal movess...
A Cow eats...
A Cow movess...
A dog eats...
A dog movess...
method overloading and method overiding
same multiple method's with different arguments is known as method overloading
There are two ways to overload the method in java
By changing number of arguments
By changing the data type
method overloading example
calculationresult.java
class Calculationresult{
void mul(int a,int b){System.out.println(a*b);}
void mul(int a,int b,int c){System.out.println(a*b*c);}
public static void main(String args[]){
Calculationresult obj=new Calculationresult();
obj.mul(10,10,10);
obj.mul(20,20);
}
}
output
1000
400
method overiding
If subclass has the same method as declared in the parent class, it is known as method overriding
in java.
car.java
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class car extends Vehicle{
void run(){System.out.println("car is running safely");}
public static void main(String args[]){
car obj = new car();
obj.run();
}}
output
car is running safely
Solution
Inheritence
Java supports inheritance and thus, variables and methods of the superclass are inherited and can
be used by the subclass. but the private members of the superclass that cannot be accessed
directly from the subclass.
inheritenceexample.java
class Animal {//super class
public Animal() {
System.out.println("A new animal has been created!");
}
public void eat() {//super class methods eat and moves
System.out.println("An animal eats...");
}
public void moves() {
System.out.println("An animal movess...");
}
}
class Cow extends Animal {//Cow subclass
public Cow() {
super();//used to invoke super class constructer
System.out.println("A new cow has been created!");
}
@Override
public void eat() {
System.out.println("A Cow eats...");
}
@Override
public void moves() {
System.out.println("A Cow movess...");
}
}
class Dog extends Animal {//sub class Dog
public Dog() {
super();
System.out.println("A new dog has been created!");
}
@Override
public void eat() {
System.out.println("A dog eats...");
}
@Override
public void moves() {
System.out.println("A dog movess...");
}
}
public class inhertenceexample {//main class
public static void main(String[] args) {
Animal animal = new Animal();
Cow Cow = new Cow();
Dog dog = new Dog();
System.out.println();
animal.eat();
animal.moves();
Cow.eat();
Cow.moves();
dog.eat();
dog.moves();
}
}
output
A new animal has been created!
A new animal has been created!
A new cow has been created!
A new animal has been created!
A new dog has been created!
An animal eats...
An animal movess...
A Cow eats...
A Cow movess...
A dog eats...
A dog movess...
method overloading and method overiding
same multiple method's with different arguments is known as method overloading
There are two ways to overload the method in java
By changing number of arguments
By changing the data type
method overloading example
calculationresult.java
class Calculationresult{
void mul(int a,int b){System.out.println(a*b);}
void mul(int a,int b,int c){System.out.println(a*b*c);}
public static void main(String args[]){
Calculationresult obj=new Calculationresult();
obj.mul(10,10,10);
obj.mul(20,20);
}
}
output
1000
400
method overiding
If subclass has the same method as declared in the parent class, it is known as method overriding
in java.
car.java
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class car extends Vehicle{
void run(){System.out.println("car is running safely");}
public static void main(String args[]){
car obj = new car();
obj.run();
}}
output
car is running safely

InheritenceJava supports inheritance and thus, variables and metho.pdf

  • 1.
    Inheritence Java supports inheritanceand thus, variables and methods of the superclass are inherited and can be used by the subclass. but the private members of the superclass that cannot be accessed directly from the subclass. inheritenceexample.java class Animal {//super class public Animal() { System.out.println("A new animal has been created!"); } public void eat() {//super class methods eat and moves System.out.println("An animal eats..."); } public void moves() { System.out.println("An animal movess..."); } } class Cow extends Animal {//Cow subclass public Cow() { super();//used to invoke super class constructer System.out.println("A new cow has been created!"); } @Override public void eat() { System.out.println("A Cow eats..."); } @Override public void moves() { System.out.println("A Cow movess..."); } } class Dog extends Animal {//sub class Dog public Dog() { super(); System.out.println("A new dog has been created!"); }
  • 2.
    @Override public void eat(){ System.out.println("A dog eats..."); } @Override public void moves() { System.out.println("A dog movess..."); } } public class inhertenceexample {//main class public static void main(String[] args) { Animal animal = new Animal(); Cow Cow = new Cow(); Dog dog = new Dog(); System.out.println(); animal.eat(); animal.moves(); Cow.eat(); Cow.moves(); dog.eat(); dog.moves(); } } output A new animal has been created! A new animal has been created! A new cow has been created! A new animal has been created! A new dog has been created! An animal eats... An animal movess... A Cow eats...
  • 3.
    A Cow movess... Adog eats... A dog movess... method overloading and method overiding same multiple method's with different arguments is known as method overloading There are two ways to overload the method in java By changing number of arguments By changing the data type method overloading example calculationresult.java class Calculationresult{ void mul(int a,int b){System.out.println(a*b);} void mul(int a,int b,int c){System.out.println(a*b*c);} public static void main(String args[]){ Calculationresult obj=new Calculationresult(); obj.mul(10,10,10); obj.mul(20,20); } } output 1000 400 method overiding If subclass has the same method as declared in the parent class, it is known as method overriding in java. car.java class Vehicle{ void run(){System.out.println("Vehicle is running");} } class car extends Vehicle{ void run(){System.out.println("car is running safely");} public static void main(String args[]){ car obj = new car(); obj.run(); }} output
  • 4.
    car is runningsafely Solution Inheritence Java supports inheritance and thus, variables and methods of the superclass are inherited and can be used by the subclass. but the private members of the superclass that cannot be accessed directly from the subclass. inheritenceexample.java class Animal {//super class public Animal() { System.out.println("A new animal has been created!"); } public void eat() {//super class methods eat and moves System.out.println("An animal eats..."); } public void moves() { System.out.println("An animal movess..."); } } class Cow extends Animal {//Cow subclass public Cow() { super();//used to invoke super class constructer System.out.println("A new cow has been created!"); } @Override public void eat() { System.out.println("A Cow eats..."); } @Override public void moves() { System.out.println("A Cow movess..."); } } class Dog extends Animal {//sub class Dog public Dog() {
  • 5.
    super(); System.out.println("A new doghas been created!"); } @Override public void eat() { System.out.println("A dog eats..."); } @Override public void moves() { System.out.println("A dog movess..."); } } public class inhertenceexample {//main class public static void main(String[] args) { Animal animal = new Animal(); Cow Cow = new Cow(); Dog dog = new Dog(); System.out.println(); animal.eat(); animal.moves(); Cow.eat(); Cow.moves(); dog.eat(); dog.moves(); } } output A new animal has been created! A new animal has been created! A new cow has been created! A new animal has been created! A new dog has been created!
  • 6.
    An animal eats... Ananimal movess... A Cow eats... A Cow movess... A dog eats... A dog movess... method overloading and method overiding same multiple method's with different arguments is known as method overloading There are two ways to overload the method in java By changing number of arguments By changing the data type method overloading example calculationresult.java class Calculationresult{ void mul(int a,int b){System.out.println(a*b);} void mul(int a,int b,int c){System.out.println(a*b*c);} public static void main(String args[]){ Calculationresult obj=new Calculationresult(); obj.mul(10,10,10); obj.mul(20,20); } } output 1000 400 method overiding If subclass has the same method as declared in the parent class, it is known as method overriding in java. car.java class Vehicle{ void run(){System.out.println("Vehicle is running");} } class car extends Vehicle{ void run(){System.out.println("car is running safely");} public static void main(String args[]){ car obj = new car();
  • 7.