Chapter 5-Oop Concepts
Chapter 5-Oop Concepts
Chapter five :
OOP concepts
Outline
Inheritance
Super
Polymorphism
Method Overriding and Overloading
The Object Class
Abstract Classes
Interfaces
Encapsulation
Inheritance in Java
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
Output
d.eat(); barking...
}} eating...
Super Keyword in Java
1.class Animal{
The super keyword 2.void eat(){System.out.println("eating...");}
can also be used to 3.}
4.class Dog extends Animal{
invoke parent class 5.void eat(){System.out.println("eating bread...");}
method. It should be 6.void bark(){System.out.println("barking...");}
7.void work(){
used if subclass 8.super.eat();
contains the same 9.bark();
10.}
method as parent 11.}
class. In other words, 12.class TestSuper2{
13.public static void main(String args[]){
it is used if method is 14.Dog d=new Dog();
overridden. 15.d.work();
Output 16.}}
eating...
barking...
3) super is used to invoke parent class constructor.
1. class Person{
2. int id; 1.void display(){System.out.println(id+" "+
3. String name; name+" "+salary);}
4. Person(int id,String name){ 2.}
3.class TestSuper5{
5. this.id=id;
4.public static void main(String[] args){
6. this.name=name; 5.Emp e1=new Emp(1,"ankit",45000f);
7. } 6.e1.display();
8. } 7.}}
9. class Emp extends Person{
10. float salary;
11. Emp(int id,String name,float salary){
12. super(id,name);//reusing parent constru
ctor Output
13. this.salary=salary; 1 ankit 45000
14. }
Polymorphism in Java
1. class Shape{
2. void draw(){System.out.println("drawing...");}
3. }
4. class Rectangle extends Shape{
5. void draw(){System.out.println("drawing rectangle...");}
6. }
7. class Circle extends Shape{
8. void draw(){System.out.println("drawing circle...");}
9. }
10. class Triangle extends Shape{
11. void draw(){System.out.println("drawing triangle...");}
12. }
13. class TestPolymorphism2{
//example3
14. public static void main(String args[]){ 1.class Animal{
2.void eat(){System.out.println("eating...");}
3.}
15. Shape s; 4.class Dog extends Animal{
5.void eat(){System.out.println("eating bread...");}
16. s=new Rectangle(); 6.}
7.class Cat extends Animal{
17. s.draw(); 8.void eat(){System.out.println("eating rat...");}
9.}
10.class Lion extends Animal{
18. s=new Circle(); 11.void eat(){System.out.println("eating meat...");}
drawing triangle...
Method Overloading and Overriding
Constructor Method
• Constructor is used to initialize • Method is used to expose
the state of an object. behaviour of an object.
• Constructor must not have • Method must have return
return type. type.
• Constructor is invoked • Method is invoked
implicitly. explicitly.
• The java compiler provides a
• Method is not provided by
default constructor if you don't
compiler in any case.
have any constructor.
• Constructor name must be same • Method name may or may
32 as the class name. not be same as class name.
Abstraction in Java
1. interface Bank{
2. float rateOfInterest();
3. }
4. class SBI implements Bank{
5. public float rateOfInterest(){return 9.15f;}
6. }
7. class PNB implements Bank{
8. public float rateOfInterest(){return 9.7f;} Output
9. } ROI: 9.15
10. class TestInterface2{
11. public static void main(String[] args){
12. Bank b=new SBI();
13. System.out.println("ROI: "+b.rateOfInterest());
Multiple inheritance in Java by interface