Oop Fundamentals
Oop Fundamentals
superclass: Person
- name: String
The subclass can: - dob: Date
Add new functionality
Use inherited functionality
Override inherited functionality
subclass: Employee
- employeeID: int
- salary: int
- startDate: Date
Terminology
Syntax :
Economy of time – When you implement Employee, you already have all
functionality of Person. No need to reimplement
Parameter passing – If you have a function that expects a Person, you
can pass an Employee, and it is still fine.
Fewer special-purpose functions for every type of Person that exists
Container classes (linked lists, binary trees, etc.) can be defined to hold a
Person, and can hold any subclass of Person
superclass: Person
- name: String
- dob: Date
subclass: Employee
- employeeID: int
- salary: int
- startDate: Date
What really happens?
Person Person
- name: String name = "John Smith"
- dob: Date dob = Jan 13, 1954
Employee
name = "Sally Halls"
is a kind of
dob = Mar 15, 1968
Employee employeeID = 37518
- employeeID: int salary = 65000
- salary: int startDate = Dec 15,
- startDate: Date 2000
Inheritance in Java
Output:
No of gears are 3
speed of bicycle is 100
seat height is 25
Types of Inheritance
{ return xx/yy;
return i+j; }
{ System.out.println(c.sum(2,2));
} System.out.println(c.mul(8,9));
} System.out.println(c.divide(2,2));
public class Main extends Calc { }
int mul(int xx , int yy) }
{
O/P?
return xx*yy;
}
class Rectdemo float GetT()
{ {
int le,be; t=(b*h) / 2;
void Sval(int a,int b) return (t);
{ }
le=a; }
be=b; class Main
} {
int GetR() public static void main(String args[])
{ {
return le*be; Tri Tr=new Tri();
} Tr.Sval(40,8);
} Tr.Sdata(10,6);
class Tri extends Rectdemo System.out.println("Area of Rectangle is
{ calculated as :" +Tr.GetR());
float b,h; System.out.println("Area of Triangle is
float t; calculated As :"+Tr.GetT());
void Sdata(int q,int r) }
{ }
b=r;
h=q; O/P?
}
class Library public void display()
{ {
String lname; System.out.println(super.lname+" and
public Library(String m) "+lname);
{ }
lname = m; public static void main(String[] args)
} {
} Main c = new Main("library name","library
public class Main extends Library id");
{ c.display();
String lname; }
public Main(String x1, String x2) }
{
super(x1); //passing argument to
parent class constructor
this.lname = x2;
}
Output:?
class even {
void display()
{
System.out.println(" Even Nos,4 2");
}
}
class odd extends even {
void display()
{
super.display();
System.out.println(" Odd Nos ,1 3");
}
}
class Main {
public static void main(String[] args)
{ Output:?
even e = new odd();
e.display();
}
}
Types of Inheritance
weeping...
barking...
eating...
class Electronics { public class Tester {
public Electronics(){ public static void main(String[] arguments) {
System.out.println("Class Electronics"); LED led = new LED();
} led.deviceType();
public void deviceType() { led.category();
System.out.println("Device Type: Electronics"); led.display_tech();
} }
} }
class Television extends Electronics {
public Television() {
System.out.println("Class Television");
} Output:?
public void category() {
System.out.println("Category - Television");
}
}
class LED extends Television {
public LED() {
System.out.println("Class LED");
}
public void display_tech() {
System.out.println("Display Technology- LED");
}
}
Types of Inheritance
Class
The class called Object
Object
Constructors and Initialization
}
Object References and Inheritance
BankAccount
anAccount name = "Craig"
accountNumber = 123456
OverdraftAccount
name = "John"
accountNumber = 3323
account1
limit = 1000.0
Polymorphism
class X{}
class B extends X{}
X a=new B();//upcasting
Polymorphism
class Bike
{
void run()
{
System.out.println("running");}
}
class Activa extends Bike
{
void run()
{
System.out.println("running safely with 60km");
}
public static void main(String args[])
{
Bike b1 = new Activa();//upcasting
Output:
b1.run(); running safely with 60km
}
}
Java Runtime Polymorphism Example: Bank
Consider a scenario where Bank is a class that provides a method to get the
rate of interest. However, the rate of interest may differ according to banks.
For example, SBI, ICICI, and AXIS banks are providing 8.4%, 7.3%, and
9.7% rate of interest.
Java Runtime Polymorphism Example: Bank
class Test
class Bank
{
{
public static void main(String args[])
float getRateOfInterest()
{
{return 0;}
Bank b;
}
b=new SBI();
class SBI extends Bank
System.out.println("SBI Rate of Interest:
{
"+b.getRateOfInterest());
float getRateOfInterest()
b=new ICICI();
{return 8.4f;}
System.out.println("ICICI Rate of Interest:
}
"+b.getRateOfInterest());
class ICICI extends Bank
b=new AXIS();
{
System.out.println("AXIS Rate of Interest:
float getRateOfInterest()
"+b.getRateOfInterest());
{return 7.3f;}
}
}
}
class AXIS extends Bank
{ O/P:
float getRateOfInterest() SBI Rate of Interest: 8.4
{return 9.7f;} ICICI Rate of Interest: 7.3
} AXIS Rate of Interest: 9.7
Java Runtime Polymorphism Example: Shape
class Shape class Test2
{ {
void draw(){System.out.println("drawing..."); public static void main(String args[])
} {
} Shape s;
class Rectangle extends Shape s=new Rectangle();
{ s.draw();
void draw() s=new Circle();
{System.out.println("drawing rectangle..."); s.draw();
} s=new Triangle();
} s.draw();
class Circle extends Shape }
{ }
void draw(){System.out.println("drawing
circle...");
}
}
class Triangle extends Shape
O/P:
{
drawing rectangle...
void draw(){System.out.println("drawing
drawing circle...
triangle...");
drawing triangle...
} }
Java Runtime Polymorphism Example: Animal
class Animal class Test3
{ {
void eat() public static void main(String[] args)
{System.out.println("eating...");} {
} Animal a;
class Dog extends Animal a=new Dog();
{ a.eat();
void eat() a=new Cat();
{System.out.println("eating bread...");} a.eat();
} a=new Lion();
class Cat extends Animal a.eat();
{ }
void eat() }
{System.out.println("eating rat...");}
}
class Lion extends Animal
{ O/P:
void eat() eating bread...
{System.out.println("eating meat..."); eating rat...
} eating meat...
}
Java Runtime Polymorphism: Multilevel Inheritance
class Animal class BabyDog extends Dog
{ {
void eat() void eat()
{ {
System.out.println("eating"); System.out.println("drinking milk");
} }
} public static void main(String args[])
class Dog extends Animal {
{ Animal a1,a2,a3;
void eat() a1=new Animal();
{ a2=new Dog();
System.out.println("eating fruits");} a3=new BabyDog();
} a1.eat();
a2.eat();
a3.eat();
}
}
O/P:
eating
eating fruits
drinking milk
Java Runtime Polymorphism: Multilevel Inheritance
class Animal
{
void eat()
{
System.out.println("animal is eating...");}
}
class Dog extends Animal
{
void eat()
{
System.out.println("dog is eating...");
}
}
class BabyDog1 extends Dog
{
public static void main(String args[])
{
Animal a=new BabyDog1(); O/P:
a.eat(); Dog is eating
}
}
Static Binding and Dynamic Binding
Connecting a method call to the method body is known as binding.
There are two types of binding:
• Static Binding (also known as Early Binding).
• Dynamic Binding (also known as Late Binding).
class Dog
{
private void eat(){System.out.println("dog is eating...");
}
public static void main(String args[])
{
Dog d1=new Dog();
d1.eat();
}
}
Static Binding and Dynamic Binding
When type of the object is determined at run-time, it is known as dynamic
binding.
class Animal{
void eat(){System.out.println("animal is eating...");}
}
• The final keyword in java is used to restrict the user. The java final
keyword can be used in many context. Final can be:
• variable
• method
• class
Java final variable: If you make any variable as final, you cannot change the
value of final variable (It will be constant).
Final Methods and Final Classes
class Bike
{
final int slimit=90;//final variable
void run()
{
slimit=400;
}
public static void main(String args[])
{
Bike o=new Bike();
o.run();
}
}
class Bike
{
final void run()
{
System.out.println("running");
}
}
class Bike
{
final void run()
{
System.out.println("running...");
}
}
class Honda extends Bike
{
public static void main(String args[])
{
new Honda().run(); Output: running...
}
}