This is the fourth session in android course that started with java syntax. This presentation have many topics like ( Inheritance, Super Keyword, Polymorphism, Abstraction, Interface.)
3. Inheritance :-
• Inheritance, process where one class acquires the properties (methods and
fields) of another.
• Final class, is simply a class that can't be extended.
• subclass (child) - the class that inherits from another class
• superclass (parent) - the class being inherited from
• Syntax :
class Parent{
.....
}
class Son extends Parent{
.....
}
4. Inheritance :-
• EX :
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
5. Inheritance :-
• Types of inheritance :
Class A Class AClass A
Class B
Class C
class B Class B Class C
Single
Multilevel
Hierarchical
6. Inheritance :-
• Types of inheritance :
Class A class B
class C
Multiple
Class A
Class B Class C
Hybrid
Class E
7. Super Keyword :-
• Super, a keyword that can be used to refer immediate parent class
instance variable, method, constructor.
• Super Variable EX :
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color)
System.out.println(super.color);//prints color of Animal class
}
}
8. Super Keyword :-
• Super Method EX :
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
void bark(){System.out.println("barking...");}
void work(){
super.eat();
bark();
}
}
9. Super Keyword :-
• Super Constructor EX :
class Animal{
Animal(){System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
super();
System.out.println("dog is created");
}
}
10. Polymorphism :-
• Compile-time polymorphism is called ‘overloading’.
• Overload, Assigning a new meaning to the function/operator.
• Overloaded functions differ in regards to, different ‘number or type of
parameter(s)’, it makes one overloaded function distinct from another. In
this way, the compiler recognizes which overloaded function is being
called.
11. Polymorphism :-
• EX :
• Overload EX :
float Add(int x, float y){
return x+y;
}
_____________________
int Add(int x, int y){
return x+y;
}
13. Polymorphism :-
• Polymorphism achieved during run-time is called ‘overriding’.
• Override, Replacing the meaning of existing function/operator.
• Final method, it cannot be overridden.
• EX :
public void move(){
System.out.println("vehicle move");
}
@Override
public void move(){
System.out.println("Car move");
}
15. Abstraction :-
• Abstraction, is the process of hiding certain details and showing only
essential information to the user achieved with either abstract classes or
interfaces.
• It is a non-access modifier, used for classes and methods
• Abstract class: is a restricted class that cannot be used to create objects (to
access it, it must be inherited from another class).
• Abstract method: can only be used in an abstract class, and it does not
have a body. The body is provided by the subclass.
16. Abstraction :-
• EX :
abstract class Sum{
public abstract int sumOfTwo(int n1, int n2);
public abstract int sumOfThree(int n1, int n2, int n3);
public void disp(){
System.out.println("Sum");
}
}
17. Interface :-
• Interface, is a blueprint of a class and contains a collection of abstract
methods.
class
class
interface
class
interface
interface
extends implements extends
18. Interface :-
• EX :
interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){return 9.15f;}
}