Skip to main content

Runtime Polymorphism-Method Overriding in javaprogramming

 Runtime polymorphism will be implimented by the help of inheritance-Method Overriding

method overriding



Declaring a method in the subclass which already exists there in the parent class is known as method overriding.To override a method in a class, another method is created in its subclass that has the same signature(name,return type and parameter list) as the original method.


Remember the following  point

  • If the method is static, that method cannot be overridden
  • If the method is declared in 'final' keyword that method cannot be overridden

Importance of ‘super’ keyword

The super keyword is used to access immediate parent class(base class) properties

If methods, variables and constructor are given the same name in the base class and the derived class, the base class property can be accessed using the super keyword.The super keyword should be used in the derived class only.Cannot be used in the main method


Method overriding-sample program

class Parent { void display() { System.out.println("This is parent class method display"); System.out.println("Parent class accessing through super keyword"); System.out.println("____________________________________"); } } class Child extends Parent { void display() { System.out.println(); System.out.println("This is child method display"); System.out.println("____________________________________"); super.display(); } } class Override { public static void main(String a[]) { Child obj=new Child(); obj.display(); } }

Observe the program and its output carefully



method overriding in java real time example program





Comments

Post a Comment

Popular posts from this blog

Introduction to java programming-very important theory notes

Introduction to java programming what is java? Java is a high level object oriented computer programming language(OOPS). Java is a general purpose programming language used in computers and other applications. Java, an object-oriented programming language developed by Sun Microsystems under the leadership of James Gosling, Bill Joy, etc., is used today in many electronic devices such as web servers, computers, and mobile phones. Although it is mostly used for web programming, it is a multi-purpose programming language that can be used to do many other things. After Oracle bought Sun Microsystems in mid-2009, Java came under Oracle's control. (Wikipedia). What is a computer? A computer is an electronic device for storing and processing huge amounts of information. What is a computer program? A computer program is a set of instructions for a computer to perform a specific task. A computer cannot do anything by itself, if it wants to do something, it must get clear instructions, the i...

JAVA BUZZWORDS

     JAVA BUZZWORDS & H istory of the development of java java buzzwords explanation     Features of java is called as Java Buzzwords Platform independent —> Run on any Environment Object oriented concept(oops) —> Abstraction,Encapsulation,Inheritance and polymorphism Simple —>  No concept of pointers,explicit memory allocation, structures,operator overloading etc  Secure —> Secure for internet application (Raise  out of boundary exception in arrays and it is strong typed language) Robust —> Early checking of error it can achieved garbage collector, exception handling Portable  —> Java program written on one environment can be executed or implemented another environment Multithreading  —> Concurrent execution of several parts of same program at same time(This will improve CPU utilization) Distributed Application —> This are software that runs on multiple computers connected  to a network at ...

How to run java program using while loop

 Java program how to print Happy New Year in 10 times(using while loop) java buzzwords static keyword in java The AI ​​Revolution- Malayalam article step by step explanation Before writing Java code Install Jdk on your system and Set the path properly. Then type the Java code in the notepad and save it as a class name with the .java extension. Select the file type 'all file' while saving. Open cmd, Change the directory where the file is saved, using the cd command. Type javac class name.java then press the enter key, If you type java and give the class name, If there is no error in the program, the output will be generated. Observe the directory change using the cd command. Give the class name and watch it compile and run. How to run java program in notepad and cmd Where to code in Java and how it works? method 1 A text editor is required to write Java code The code can be run using the command prompt method 2  using an integrated development environment (IDE), such as Eclips...

Oops concepts in java

oops concepts in java with example programs Oops concepts in java Oops concept is very very important to make java programming easier and increase your programming skill.Four pillars of oops are Abstraction,Encapsulation,Inheritance,Polymorphism.Another technique to carefully understand in Java is classes and objects. This article covers very simple notes for Java programming beginners. What is object and class? The basics of oops is class and object Class is a blueprint which defines some properties and behaviors.  An object is an instance of a class which has those properties and behaviors attached.A class is not allocated memory when it is defined. An object is allocated memory when it is created Class and object —> object is defined as real word entities .Object consists of property and some task Class is a blueprint that object follows, Class  consists of many number of objects Four pillars of oops are Abstraction   —> Means showing only essential parts and hi...

Command Line Arguments In Java

Command Line Arguments In Java No input statement needed in the program itself. we can pass the input as an argument while running the program. Sometimes you will want to pass information into a program when you run it. This is accomplished by passing command line arguments to main function. To access the command line arguments inside a java program is quite easy are stored as string in string array passed to main function. Example program of command line arguments in java class CommandLineExample {    public static void main(String args[]) {    System.out.println("\nMy first input is  :"+args[0]);    }    }    Addition of two numbers in java using command line arguments  Example:write a program add two integer number    class AddCommandLine { public static void main(String args[]) { int num1=Integer.parseInt(args[0]); //type cast int num2=Integer.parseInt(args[1]); //type ...

Keywords in java programming

Keywords in java programming Keyword is a predefined word given by the compiler which has some specific task. Keywords also known as reserved word. All the java technology keywords are lower case . Java programming has a richer set of keywords than C language or C ++ language. Some java keywords are import —>In java programming implementing classes and methods import the packages that purpose using the keyword import. class —> In order to create a class we have use the keyword class to see an example written using the class keyword ) this —> This keyword is used to represent the method belongs to particular current object Super —> It can used in inheritance concept extends —> Also used in inheritance concepts. Accessing methods one class to another class that can done by extends keyword package —> In java there will be package which consists of number of classes and interfaces in order to access that package, using the key word package return —> Return key wor...

Naming Convention In Java

Naming Convention In Java-camel case example Naming Conventions are rules to be followed by giving a name to the identifier.Identifiers are program elements(such as class,object,variable,method so on ).For building software you should follow this rule to make your code look good, more efficient and more readble.Java has certain rules for naming identifier. This are given below Digit, letters, underscore or currency symbols( £, €, $ ) are allowed                The first letter of any identifier cannot be a digit An identifier name must not be a reserved words or keywords Java follows camelCase rule what is camelcase rule? CamelCase is a way to separate the words in a phrase by making the first letter of each word capitalized and not using spaces (ex :- MyFirstJavaClass) Class name —> should begin with a capital letter(In a single word) If it is multiple words, the first letter of every word begins with a capital letter. Ex :- class Stu...