Core Java Training
OOPs with Java & Exception Handling
Page 1Classification: Restricted
Agenda
• Overview of OOP continued…
• Abstraction – using Abstract Classes and Interfaces.
Java & JEE Training
Review of OOPs concepts (Last class)
Page 3Classification: Restricted
Difference between method overloading and overriding?
• Explain.
Page 4Classification: Restricted
Point to ponder..
Can we override static methods in Java?
Page 5Classification: Restricted
Point to ponder:
Can constructors be declared final?
Page 6Classification: Restricted
Abstraction using abstract class and method
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely..");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
Page 7Classification: Restricted
Example:
abstract class Shape{
abstract void draw();
}
//In real scenario, implementation is provided by others i.e. unknown by end user
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
//In real scenario, method is called by programmer or user
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();//In real scenario, object is provided through method e.g. getShape()
method
s.draw();
}
}
Page 8Classification: Restricted
Example:
abstract class Bank{
abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
}
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}}
Page 9Classification: Restricted
Abstract class rule…
If there is any abstract method in a class, that class must be abstract.
class Bike12{
abstract void run();
}
//Compile time error
Page 10Classification: Restricted
Abstract class rule
If you are extending any abstract class that have abstract method, you must
either provide the implementation of the method or make this class abstract.
Page 11Classification: Restricted
Abstraction with interfaces
interface A{
void a();
void b();
void c();
void d();
}
abstract class B implements A{
public void c(){System.out.println("I am C");}
}
class M extends B{
public void a(){System.out.println("I am a");}
public void b(){System.out.println("I am b");}
public void d(){System.out.println("I am d");}
}
class Test5{
public static void main(String args[]){
A a=new M();
a.a();
a.b();
a.c();
a.d();
}}
Page 12Classification: Restricted
Interfaces in Java
• An interface in java is a blueprint of a class. It has static constants and
abstract methods only.
• The interface in java is a mechanism to achieve fully abstraction.
• The java compiler adds public and abstract keywords before the interface
method and public, static and final keywords before data members.
Page 13Classification: Restricted
Why use Java interface?
• It is used to achieve fully abstraction.
• By interface, we can support the functionality of multiple inheritance.
• It can be used to achieve loose coupling.
Page 14Classification: Restricted
Relationship between classes and interfaces
Page 15Classification: Restricted
Simple interface example
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
Page 16Classification: Restricted
Multiple inheritance through interfaces
Page 17Classification: Restricted
Multiple inheritance example
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}
Page 18Classification: Restricted
Interface inheritance example
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class Testinterface2 implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
Testinterface2 obj = new Testinterface2();
obj.print();
obj.show();
}
}
Page 19Classification: Restricted
Point to ponder…
Multiple inheritance is not supported through class in java but it is possible
by interface, why?
Page 20Classification: Restricted
Topics to be covered in next session
• Packaging Classes & Access Modifiers
Page 21Classification: Restricted
Thank You!

OOP with Java - Abstract Classes and Interfaces

  • 1.
    Core Java Training OOPswith Java & Exception Handling
  • 2.
    Page 1Classification: Restricted Agenda •Overview of OOP continued… • Abstraction – using Abstract Classes and Interfaces.
  • 3.
    Java & JEETraining Review of OOPs concepts (Last class)
  • 4.
    Page 3Classification: Restricted Differencebetween method overloading and overriding? • Explain.
  • 5.
    Page 4Classification: Restricted Pointto ponder.. Can we override static methods in Java?
  • 6.
    Page 5Classification: Restricted Pointto ponder: Can constructors be declared final?
  • 7.
    Page 6Classification: Restricted Abstractionusing abstract class and method abstract class Bike{ abstract void run(); } class Honda4 extends Bike{ void run(){System.out.println("running safely..");} public static void main(String args[]){ Bike obj = new Honda4(); obj.run(); } }
  • 8.
    Page 7Classification: Restricted Example: abstractclass Shape{ abstract void draw(); } //In real scenario, implementation is provided by others i.e. unknown by end user class Rectangle extends Shape{ void draw(){System.out.println("drawing rectangle");} } class Circle1 extends Shape{ void draw(){System.out.println("drawing circle");} } //In real scenario, method is called by programmer or user class TestAbstraction1{ public static void main(String args[]){ Shape s=new Circle1();//In real scenario, object is provided through method e.g. getShape() method s.draw(); } }
  • 9.
    Page 8Classification: Restricted Example: abstractclass Bank{ abstract int getRateOfInterest(); } class SBI extends Bank{ int getRateOfInterest(){return 7;} } class PNB extends Bank{ int getRateOfInterest(){return 8;} } class TestBank{ public static void main(String args[]){ Bank b; b=new SBI(); System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %"); b=new PNB(); System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %"); }}
  • 10.
    Page 9Classification: Restricted Abstractclass rule… If there is any abstract method in a class, that class must be abstract. class Bike12{ abstract void run(); } //Compile time error
  • 11.
    Page 10Classification: Restricted Abstractclass rule If you are extending any abstract class that have abstract method, you must either provide the implementation of the method or make this class abstract.
  • 12.
    Page 11Classification: Restricted Abstractionwith interfaces interface A{ void a(); void b(); void c(); void d(); } abstract class B implements A{ public void c(){System.out.println("I am C");} } class M extends B{ public void a(){System.out.println("I am a");} public void b(){System.out.println("I am b");} public void d(){System.out.println("I am d");} } class Test5{ public static void main(String args[]){ A a=new M(); a.a(); a.b(); a.c(); a.d(); }}
  • 13.
    Page 12Classification: Restricted Interfacesin Java • An interface in java is a blueprint of a class. It has static constants and abstract methods only. • The interface in java is a mechanism to achieve fully abstraction. • The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members.
  • 14.
    Page 13Classification: Restricted Whyuse Java interface? • It is used to achieve fully abstraction. • By interface, we can support the functionality of multiple inheritance. • It can be used to achieve loose coupling.
  • 15.
    Page 14Classification: Restricted Relationshipbetween classes and interfaces
  • 16.
    Page 15Classification: Restricted Simpleinterface example interface printable{ void print(); } class A6 implements printable{ public void print(){System.out.println("Hello");} public static void main(String args[]){ A6 obj = new A6(); obj.print(); } }
  • 17.
    Page 16Classification: Restricted Multipleinheritance through interfaces
  • 18.
    Page 17Classification: Restricted Multipleinheritance example interface Printable{ void print(); } interface Showable{ void show(); } class A7 implements Printable,Showable{ public void print(){System.out.println("Hello");} public void show(){System.out.println("Welcome");} public static void main(String args[]){ A7 obj = new A7(); obj.print(); obj.show(); } }
  • 19.
    Page 18Classification: Restricted Interfaceinheritance example interface Printable{ void print(); } interface Showable extends Printable{ void show(); } class Testinterface2 implements Showable{ public void print(){System.out.println("Hello");} public void show(){System.out.println("Welcome");} public static void main(String args[]){ Testinterface2 obj = new Testinterface2(); obj.print(); obj.show(); } }
  • 20.
    Page 19Classification: Restricted Pointto ponder… Multiple inheritance is not supported through class in java but it is possible by interface, why?
  • 21.
    Page 20Classification: Restricted Topicsto be covered in next session • Packaging Classes & Access Modifiers
  • 22.