Inheritance in Java
Inheritance in Java
can be achieved).
For Code Reusability.
C o = new c()
0.msg();
Super.msg();
Aggregation in Java
If a class have an entity reference, it is known as
Aggregation. Aggregation represents HAS-A relationship.
Consider a situation, Employee object contains many
informations such as id, name, emailId etc. It contains
one more object named address, which contains its own
informations such as city, state, country, zipcode etc. as
given below.
1. class Employee{
2. int id;
3. String name;
4. Address address;//Address is a class
5. ...
6. }
In such case, Employee has an entity reference address,
so relationship is Employee HAS-A address.
Why use Aggregation?
For Code Reusability.
5. this.city = city;
6. this.state = state;
7. this.country = country;
8. }
9.
10. }
Emp.java
1. public class Emp {
2. int id;
3. String name;
4. Address address;
5.
6. public Emp(int id, String name,Address address) {
7. this.id = id;
8. this.name = name;
9. this.address=address;
10. }
11.
12. void display(){
13. System.out.println(id+" "+name);
14. System.out.println(address.city+" "+address.state+
" "+address.country);
15. }
16.
17. public static void main(String[] args) {
18. Address address1=new Address("gzb","UP","india")
;
19. Address address2=new Address("gno","UP","india")
;
20.
21. Emp e=new Emp(111,"varun",address1);
22. Emp e2=new Emp(112,"arun",address2);
23.
24. e.display();
25. e2.display();
26.
27. }
28. }
Test it Now
Output:111 varun
gzb UP india
112 arun
gno UP india
Method Overriding in Java
1. class Bank{
2. int getRateOfInterest(){return 0;}
3. }
4.
5. class SBI extends Bank{
6. int getRateOfInterest(){return 8;}
7. }
8.
9. class ICICI extends Bank
10. {
11. int getRateOfInterest(){return 7;}
12. }
13. class AXIS extends Bank
14. {
15. int getRateOfInterest()
16. {
17. return 9;}
18. }
19.
20. class Test2{
21. public static void main(String args[]){
22. SBI s=new SBI();
23. ICICI i=new ICICI();
24. AXIS a=new AXIS();
25. System.out.println("SBI Rate of Interest: "+s.getRat
eOfInterest());
26. System.out.println("ICICI Rate of Interest: "+i.getR
ateOfInterest());
27. System.out.println("AXIS Rate of Interest: "+a.getR
ateOfInterest());
28. }
29. }
Test it Now
Output:
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9
Can we override static method?
No, static method cannot be overridden. It can be
proved by runtime polymorphism, so we.
Why we cannot override static method?
because static method is bound with class whereas
instance method is bound with object. Static belongs to
class area and instance belongs to heap area.
8. }
9. public static void main(String args[]){
10. Bike3 b=new Bike3();
11. b.display();
12. }
13. }
Test it Now
Output:100
In the above example Vehicle and Bike both class
have a common property speed. Instance variable
of current class is refered by instance bydefault,
but I have to refer parent class instance variable
that is why we use super keyword to distinguish
between parent class instance variable and current
class instance variable.
10. }
11.
12. {System.out.println("instance initializer block is inv
oked");}
13.
14. public static void main(String args[]){
15. B2 b=new B2();
16. }
17. }
Test it Now
Output:parent class constructor invoked
instance initializer block is invoked
child class constructor invoked
<<prevnext>>
← prevnext →
Polymorphism in Java
Polymorphism in java is a concept by which we
can perform a single action by different ways.
Polymorphism is derived from 2 greek words: poly
and morphs. The word "poly" means many and
"morphs" means forms. So polymorphism means
many forms.
There are two types of polymorphism in java: compile
time polymorphism and runtime polymorphism. We
can perform polymorphism in java by method
overloading and method overriding.
If you overload static method in java, it is the
example of compile time polymorphism. Here, we will
focus on runtime polymorphism in java.
1. class A{}
2. class B extends A{}
1. A a=new B();//upcasting
static binding
When type of the object is determined at compiled
time(by the compiler), it is known as static binding.
If there is any private, final or static method in a
class, there is static binding.
Example of static binding
1. class Dog{
2. private void eat(){System.out.println("dog is eating...
");}
3.
4. public static void main(String args[]){
5. Dog d1=new Dog();
6. d1.eat();
7. }
8. }
Dynamic binding
When type of the object is determined at run-time, it
is known as dynamic binding.
Example of dynamic binding
1. class Animal{
2. void eat(){System.out.println("animal is eating...");}
3. }
4.
5. class Dog extends Animal{
6. void eat(){System.out.println("dog is eating...");}
7.
8. public static void main(String args[]){
9. Animal a=new Dog();
10. a.eat();
11. }
12. }
Test it Now
Output:dog is eating...
In the above example object type cannot be
determined by the compiler, because the instance
of Dog is also an instance of Animal.So compiler
doesn't know its type, only its base type.
Next TopicDowncasting and instanceof operator
<<prevnext>>
Java instanceof
1. java instanceof
2. Example of instanceof operator
3. Applying the instanceof operator with a variable
the have null value
4. Downcasting with instanceof operator
5. Downcasting without instanceof operator
The java instanceof operator is used to test
whether the object is an instance of the specified type
(class or subclass or interface).
The instanceof in java is also known as
type comparison operatorbecause it compares the
instance with type. It returns either true or false. If
we apply the instanceof operator with any variable
that has null value, it returns false.
Simple example of java instanceof
8. }
9. }
10.
11. public static void main (String [] args) {
12. Animal a=new Dog3();
13. Dog3.method(a);
14. }
15.
16. }
Test it Now
Output:ok downcasting performed
Downcasting without the use of java instanceof
Downcasting can also be performed without the use
of instanceof operator as displayed in the following
example:
1. class Animal { }
2. class Dog4 extends Animal {
3. static void method(Animal a) {
4. Dog4 d=(Dog4)a;//downcasting
5. System.out.println("ok downcasting performed");
6. }
7. public static void main (String [] args) {
8. Animal a=new Dog4();
9. Dog4.method(a);
10. }
11. }
Test it Now
Output:ok downcasting performed
Let's take closer look at this, actual object that is
referred by a, is an object of Dog class. So if we
downcast it, it is fine. But what will happen if we
write:
1. Animal a=new Animal();
2. Dog.method(a);
3. //Now ClassCastException but not in case of instanceo
f operator
Understanding Real use of instanceof in java
Let's see the real use of instanceof keyword by the
example given below.
1. interface Printable{}
2. class A implements Printable{
3. public void a(){System.out.println("a method");}
4. }
5. class B implements Printable{
6. public void b(){System.out.println("b method");}
7. }
8.
9. class Call{
10. void invoke(Printable p){//upcasting
11. if(p instanceof A){
12. A a=(A)p;//Downcasting
13. a.a();
14. }
15. if(p instanceof B){
16. B b=(B)p;//Downcasting
17. b.b();
18. }
19.
20. }
21. }//end of Call class
22.
23. class Test4{
24. public static void main(String args[]){
25. Printable p=new B();
26. Call c=new Call();
27. c.invoke(p);
28. }
29. }
Test it Now
Output: b method
Next TopicAbstract class in java
<<prevnext>>
Abstraction in Java
Abstraction is a process of hiding the
implementation details and showing only functionality
to the user.
Another way, it shows only important things to the
user and hides the internal details for example
sending sms, you just type the text and send the
message. You don't know the internal processing
about the message delivery.
Abstraction lets you focus on what the object does
instead of how it does it.
Ways to achieve Abstaction
There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
2. Interface (100%)
abstract method
A method that is declared as abstract and does not
have implementation is known as abstract method.
1. interface A{
2. void a();
3. void b();
4. void c();
5. void d();
6. }
7.
8. abstract class B implements A {
9. public void c(){System.out.println("I am C");}
10. }
11.
12. class M extends B{
13. public void a(){System.out.println("I am a");}
14. public void b(){System.out.println("I am b");}
15. public void d(){System.out.println("I am d");}
16. }
17.
18. class Test5{
19. public static void main(String args[]){
20. A a=new M();
21. a.a();
22. a.b();
23. a.c();
24. a.d();
25. }}
Test it Now
Output: I am a
I am b
I am c
I am d