Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

VIT BHOPAL UNIVERSITY

PRESENTATION ON:-

Method Overriding

Abstract Class

Abstract Method. Presented by:-


AKSHAY SURANGE
REG NO- 19MCA10049
◦ Method Overriding
◦ Abstraction
◦ Abstract Class
◦ Abstract Method

Contents:-
METHOD OVERRIDING
◦ Declaring a method in child class which is already present in the parent class is called
Method Overriding.
◦ In simple words, overriding means to override the functionality of an existing method.
◦ Assume we have multiple child classes. In case one of the child classes want to use the
parent class method and other class want to use their own implementation then we
can use overriding feature.
Conditions for Overriding
◦ Methods of both parent and child class must have the same name.
◦ Methods must have the same argument list and return type.
◦ A method declared static cannot be overridden.
◦ If a method cannot be inherited, it cannot be overridden.
◦ Constructors cannot be overridden
Example
◦ public class Akshay {
◦ void name()
◦{
◦ System.out.println("AKSHAY");
◦}
◦}
◦ class Akki extends Akshay
◦{
◦ void name()
◦{
◦ System.out.println("Hello Akshay");
◦}
◦}
◦ class m
◦ {
◦ public static void main(String args[])
◦ {
◦ Akki obj = new Akki();
◦ obj.name();
◦ }
◦ }
Advantage Of Method Overriding
◦ The main advantage of method overriding is that the class can give its own specific
implementation to a inherited method without even modifying the parent class code.
◦ This is helpful when a class has several child classes, so if a child class needs to use the
parent class method, it can use it and the other classes that want to have different
implementation can use overriding feature to make changes without touching the
parent class code.
Abstraction
◦ Data Abstraction is the property by virtue of which only the essential details are
displayed to the user. The trivial or the non-essentials units are not displayed to the user.
Ex: A car is viewed as a car rather than its individual components.
◦ Data Abstraction may also be defined as the process of identifying only the required
characteristics of an object ignoring the irrelevant details. The properties and behaviors
of an object differentiate it from other objects of similar type and also help in
classifying/grouping the objects.
Abstract Class
◦ “Abstract” keyword is used to declare abstract class in java. These classes are used to
achieve the concept of abstraction. Some important rules are to be kept in mind while
working with abstract class in Java.
◦ Abstract classes are classes that contain one or more abstract methods. An abstract
method is a method that is declared, but contains no implementation.
Rules
◦ We can declare an abstract class using the abstract keyword.
◦ We can have abstract and non-abstract methods in an abstract class.
◦ An abstract class can have constructors and static methods.
◦ An abstract class can have final methods which will force the subclass to not change
the body of the method.
◦ We cant instantiate an abstract class.
Example
◦ abstract class Base {
◦ abstract void Hello ();
◦ }
◦ class D extends Base{
◦ void Hello() { System.out.println("Hello Akshay"); }
◦ }
◦ class Main {
◦ public static void main(String args[]) {


◦ Base b = new D();
◦ b.Hello();
◦ }
◦ }
Output
Advantages
◦ The advantage of using an abstract class is that you can group several related classes
together .
◦ Grouping classes together is important in keeping a program organized and
understandable.
Abstract Method
◦ Abstract methods, similar to methods within an interface, are declared without any
implementation. They are declared with the purpose of having the child class provide
implementation. They must be declared within an abstract class.
◦ A class declared abstract may or may not include abstract methods. They are created
with the purpose of being a super class.
Example
◦ abstract class Myclass {
◦ public void disp(){
◦ System.out.println("Hello Java");
◦ }
◦ abstract public void disp2();
◦ }

◦ class Demo extends Myclass{



◦ public void disp2()
◦ {
◦ System.out.println("Hello Akshay");
◦ }
◦ public static void main(String args[]){
◦ Demo obj = new Demo();
◦ obj.disp2();
◦ }
◦ }
Output
THANK YOU

Guided By:-
Dr. Kanchan Lata Kashyap

You might also like