6. What is inheritance?
Inheritance is a fundamental Object Oriented
concept
A class can be defined as a "subclass" of another
class.
The subclass inherits all data attributes of its
superclass
The subclass inherits all methods of its
superclass
The subclass can:
Add new functionality
Use inherited functionality
Override inherited functionality
7. Benefit of using inheritance
A code can be used again and again
Inheritance in Java enhances the properties of the
class, which means that property of the parent class
will automatically be inherited by the base class
It can define more specialized classes by adding
new details.
8. keyword
Keyword:
Inheritance is declared using the "extends" keyword
To access Super Class from Sub Class we use super keyword
9. EXAMPLE
public class SuperClass {
Scanner sc = new Scanner(System.in); // Here we take input from
Keyboard
public double CircleArea(){
double pi=3.1416,num1=0,r=0,CircleAreaResult=0;
num1=sc.nextDouble(); // num1 input taken
r=sc.nextDouble(); // r input taken
CircleAreaResult = pi*num1*(r*r);
return CircleAreaResult;
}
public double TriangleArea(){
double base,hight,TriangleAreaResult = 0;
base=sc.nextDouble(); // Here base input taken keyboard
hight=sc.nextDouble(); // hight input taken from keyborad
TriangleAreaResult = 0.5*base*hight;
return TriangleAreaResult;
}
10. EXAMPLE(continue)
public void Result() {
double Result1,Result2;
Result1= CircleArea();
Result2= TriangleArea();
System.out.println(“Circle Area : “+Result1+”nTriangle Area :
“+Result2);
}
}
public class Main extends SuperClass{
public static void main(String[] args) {
SuperClass sp= new SuperClass();
sp.Result();
}
}
11. Access Modifiers
Private data fields are not accessible to derived classes
Protected visibility allows data fields to be accessed either by the class
defining it or any subclass.
In general, it is better to use private visibility because
subclasses may be written by different programmers and
it is always good practice to restrict and control access to
the superclass data fields
Members (variables and methods) declared with public
visibility are accessible, and those with private visibility
are not.
12. Polymorphism
Polymorphism come from the two Greek words
‘poly’ meaning many and ‘morphs” meaning forms.
The ability to exist in different form is called
polymorphism. The same variable or method can
perform different tasks; the programmer has the
advantage of writing flexible code.
13. Method Overloading
When 2 or more methods in a class have the same method names with
different arguments, it is said to be method overloading.
Overloading does not block inheritance from the superclass.
Overloaded methods must have different method
signatures.
15. Method Overriding
When a method in a class has the same method name with same
arguments as that of the superclass, it is said to be method overriding.
Overriding blocks inheritance from the superclass.
Overridden methods must have same signature.
17. Abstruct class and method
A class that is declared abstract
Ex - abstract class Demo
It may or may not use abstract
methods
A method that is declared abstract
A method that is declared without an
implementation
Ex - abstract void add(int x, int y);
18. Example
public abstract class Shape {
//Abstract method i.e no implementation
abstract void Area();
}
public class Rectangle extends Shape {
@Override
void Area() {
Double area = length * width;
}
}
public class Circle extends Shape {
@Override
void Area() {
Double area = 3.14 * radius*radius;
}
}
19. Interface
Interfaces are declared using the interface keyword
Interface can contain :
method signature
(no implementation)
constant declarations
(variable declarations that are declared
to be both static and final)
21. Example
interface MyInterface
{
public void method1();
public void method2();
}
class XYZ implements MyInterface
{
public void method1() { System.out.println("implementation of method1"); }
public void method2() { System.out.println("implementation of method2"); }
public static void main(String arg[]) {
MyInterface obj = new XYZ(); obj. method1();
}
}
Output:
implementation of method1
22. Difference Between Interface and Abstract Class
Main difference is methods of a Java interface are implicitly abstract and
cannot have implementations. A Java abstract class can have instance
methods that implements a default behavior.
Variables declared in a Java interface is by default final. An abstract class
may contain non-final variables.
Java interface should be implemented using keyword “implements”; A Java
abstract class should be extended using keyword “extends”.
An interface can extend another Java interface only, an abstract class can
extend another Java class and implement multiple Java interfaces.
A Java class can implement multiple interfaces but it can extend only one
abstract class.
Interface is absolutely abstract and cannot be instantiated; A Java abstract
class also cannot be instantiated, but can be invoked if a main() exists.