Skip to main content

Java Method Overriding

Java Method Overriding

Declaring a method in the subclass which already exists there in the parent class is known as method overriding. When a class is inheriting a method from a superclass of its own, then there is an option of overriding the method provided it is not declared as final. The advantage of using overriding is the ability to classify a behavior that’s specific to the child class and the child class can implement a parent class method based on its necessity.

There are certain rules that a programmer should follow in order to implement overriding. These are:
  • In Java, a method can only be written in the child class and not in same class.
  • Argument list should be exactly the same as that of the overridden method of that class.
  • Instance methods can also be overridden if they are inherited by the child class.
  • A constructor cannot be overridden.
  • Final – declared methods cannot be overridden.
  • Any method that is static cannot be used to override.
  • The return type must have to be the same or a subtype of the return type declared in the original overridden method in the parent class.
  • If a method cannot be inherited then it cannot be overridden.
  • A child class within the same package as the instance’s parent class can override any parent class method that is not declared private or final.
  • A child class in a different package can only override the non-final methods declared as public or protected.

Java Program to Demonstrate Method Overriding

Example:
class college {
public void move() {
System.out.println("College is open");
}
}
class univ extends college {
public void move() {
System.out.println("University is open too");
}
}
public class stud {
public static void main(String args[]) {
college a
= new college();
college b
= new univ();
a
.move();
b
.move();
}
}

Comments

For Programs Click Here

Popular posts from this blog

C++ Constructors and Destructors

C++ Constructors and Destructors Providing the initial value as described in the earlier chapters of C++ does not conform to the philosophy of C++. So C++ provides a special member function called the constructor which enables an object to initialize itself at the time of its creation. This is known as automatic initialization of objects. This concept of C++ also provides another member function called destructor which is used to destroy the objects when they are no longer required. In this chapter, you will learn about how constructors and destructors work, types of constructors and how they can be implemented within C++ program. What are constructors? The process of creating and deleting objects in C++ is vital task. Each time an instance of a class is created the constructor method is called. Constructors is a special member functions of class and it is used to initialize the objects of its class. It is treated as a special member function because its name is the same as the cla...

Python Tutorial

               Python Tutorials             Python Tutorial Python is a general purpose object-oriented programming language with high-level programming capabilities. This Python tutorial series will help you to get started in Python programming language from scratch. Prerequisites To learn Python Programming Language you haven’t required any previous programming knowledge, but the basic understanding of any other programming languages will help you to understanding the Python Programming Concepts quickly. Python Programming Example A quick look at the example of  Hello, World!  in Python programming, and detailed description is given in the  Python Basics  page. Example: #!/usr/bin/python print "Hello, World!" Output: Hello, World! The above example has been used to print  Hello, World!  text on the screen.