Professional Documents
Culture Documents
CMJD Class Note 04 - Object Oriented Programming
CMJD Class Note 04 - Object Oriented Programming
class Vehicle{
public void park(){
//
}
}
class Car extends Vehicle{
}
class Bus extends Vehicle{
}
class Van extends Vehicle{
}
class Demo{
Page 1 of 18
CMJD | Comprehensive Master Java Developer
02 From 01 (Illegal)
class Vehicle{
public void park();
}
class Car extends Vehicle{
public void park(){
System.out.println("Car Parking..");
}
}
class Bus extends Vehicle{
public void park(){
System.out.println("Bus Parking..");
}
}
class Van extends Vehicle{
public void park(){
System.out.println("Van Parking..");
}
}
class Demo{
public static void main(String args[]){
Vehicle[] vehicles={new Car(),new Van(),new Car(),new Bus()};
for(Vehicle v1 : vehicles){
v1.park(); //Single interface, many forms==>Polymorphism
}
}
}
Page 2 of 18
CMJD | Comprehensive Master Java Developer
}
class Van extends Vehicle{
public void park(){
System.out.println("Van Parking..");
}
}
class Demo{
public static void main(String args[]){
Vehicle[] vehicles={new Car(),new Van(),new Car(),new Bus()};
for(Vehicle v1 : vehicles){
v1.park(); //Single interface, many forms==>Polymorphism
}
}
}
04 Case I
class Vehicle{
public void park(); //Illegal
}
05 Case II
06 Case III
abstract class Vehicle{
abstract public void park(){ //Illegal
}
}
Page 3 of 18
CMJD | Comprehensive Master Java Developer
07 Case IV
}
}
08 Case V
09 Case VI
Page 4 of 18
CMJD | Comprehensive Master Java Developer
10 Case VII
}
class Demo{
public static void main(String args[]){
Vehicle v1; //Legal
v1=new Vehicle(); //Illegal
}
}
11 Case VIII
class Demo{
public static void main(String args[]){
Vehicle v1=new Car();
v1.park();
}
}
Page 5 of 18
CMJD | Comprehensive Master Java Developer
12 Case IX
13 Case X
14 Case XI
15 Case XII
class Animal{
public void eat(){
//
}
}
abstract class Lion extends Animal{
abstract public void eat();
}
class Dog extends Animal{} //Legal
class Cat extends Lion{} //Illegal
Page 6 of 18
CMJD | Comprehensive Master Java Developer
Java Interfaces
16.
interface Vehicle{
public void park();
public void start();
public void stop();
}
17.
interface Vehicle{
public void park();
}
//class Car extends Vehicle{} //Wrong
class Car implements Vehicle{
public void park(){
System.out.println("Car Parking..");
}
}
class Demo{
public static void main(String args[]){
Vehicle v1=new Car();
v1.park();
}
}
Page 7 of 18
CMJD | Comprehensive Master Java Developer
18 Case I
interface Vehicle{
}
//class Car extends Vehicle{} // Wrong
class Car implements Vehicle{} // Correct
19 Case II
20 Case III
21 Case IV
interface Vehicle{
void park(); // Implicitly public
public void start(); //
protected void stop(); // Illegal
}
22 Case V
interface Vehicle{
public void start(){ // Illegal
//body
}
}
Page 8 of 18
CMJD | Comprehensive Master Java Developer
23 Case VI
interface Vehicle{
public void start();
public void park();
}
class Car implements Vehicle{}
class Van implements Vehicle{
public void start(){
//body
}
}
class Jeep implements Vehicle{
public void park(){
//body
}
}
class Bus implements Vehicle{
public void park(){
//body
}
public void start(){
//body
}
}
abstract class MB implements Vehicle{
24 Case VII
interface Vehicle{
public void park();
}
class Demo{
public static void main(String args[]){
Vehicle v1; //Legal
v1=new Vehicle();
}
}
Page 9 of 18
CMJD | Comprehensive Master Java Developer
25 Case VIII
interface Vehicle{
public void park();
}
class Car implements Vehicle{
public void park(){
System.out.println("Car Parking...");
}
}
class Demo{
public static void main(String args[]){
Vehicle v1; //Legal
v1=new Car();
}
}
//Car IS-A Vehicle
//Car - Subclass
//Vehicle->Super interface
26 Case IX
interface Vehicle{
int year=1999; //implicitly final, static and public
public void park();
}
class Demo{
public static void main(String args[]){
System.out.println(Vehicle.year);//prints 1999
Vehicle.year=2000; //Illegal, final
}
}
27 Case X
interface Vehicle{
int year=1999;
Vehicle(){ //Illegal
}
public void park();
}
Page 10 of 18
CMJD | Comprehensive Master Java Developer
interface Vehicle{
public void park();
}
class Car implements Vehicle{
public void park(){
System.out.println("Car Parking..");
}
}
class Bus implements Vehicle{
public void park(){
System.out.println("Bus Parking..");
}
}
class Van implements Vehicle{
public void park(){
System.out.println("Van Parking..");
}
}
class Demo{
public static void main(String args[]){
Vehicle[] vehicles={new Car(),new Van(),new Car(),new Bus()};
for(Vehicle v1 : vehicles){
v1.park(); //Single interface, many forms==>Polymorphism
}
}
}
interface Date{
public void setDate(int year, int month, int day);
public void setYear(int year);
public void setMonth(int month);
public void setDay(int day);
public int getYear();
public int getMonth();
public int getDay();
public void printDate();
}
Page 11 of 18
CMJD | Comprehensive Master Java Developer
}
public void setDate(int year, int month, int day){
setYear(year);
setMonth(month);
setDay(day);
}
public void setYear(int year){
this.year=year;
}
public void setMonth(int month){
this.month=month;
}
public void setDay(int day){
this.day=day;
}
public int getYear(){
return year;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
Page 12 of 18
CMJD | Comprehensive Master Java Developer
class Demo{
public static void main(String args[]){
Date d1=new DateImpl();
d1.setDate(1999,12,31);
d1.printDate();
}
}
30 Case XI
class Animal{
}
interface Lion{
}
class Dog extends Animal implements Lion{ //Legal->Multiple inheritance
}
//Dog IS-A Lion
//Dog IS-AN Animal
Page 13 of 18
CMJD | Comprehensive Master Java Developer
31.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
interface WaterLevelObserver{
public void update(int waterLevel);
}
class SMSWindow extends JFrame implements WaterLevelObserver{
private JLabel smsLabel;
SMSWindow(){
setSize(300,300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("SMS Window");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
//-----------------------------------------
smsLabel=new JLabel("0");
smsLabel.setFont(new Font("",1,25));
add(smsLabel);
setVisible(true);
}
public void update(int waterLevel){
smsLabel.setText("Sending SMS : "+waterLevel);
}
}
class DisplayWindow extends JFrame implements WaterLevelObserver{
private JLabel displayLabel;
DisplayWindow(){
setSize(300,300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("Display Window");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
//-----------------------------------------
displayLabel=new JLabel("0");
displayLabel.setFont(new Font("",1,25));
add(displayLabel);
setVisible(true);
}
Page 14 of 18
CMJD | Comprehensive Master Java Developer
Page 15 of 18
CMJD | Comprehensive Master Java Developer
this.waterTankController=waterTankController;
//-----------------------------------------
waterLevelSlider=new JSlider(JSlider.VERTICAL,0,100,50);
waterLevelSlider.setFont(new Font("",1,25));
waterLevelSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent ce) {
int waterLevel=waterLevelSlider.getValue();
waterTankController.setWaterLevel(waterLevel);
}
});
add(waterLevelSlider);
}
}
class WaterTankController{
private WaterLevelObserver[] observerArray=new WaterLevelObserver[0];
private int waterLevel;
public void addWaterLevelObserver(WaterLevelObserver waterLevelObserver){
WaterLevelObserver[] temp=new WaterLevelObserver[observerArray.length+1];
for(int i=0; i<observerArray.length; i++){
temp[i]=observerArray[i];
}
temp[observerArray.length]=waterLevelObserver;
observerArray=temp;
}
public void setWaterLevel(int waterLevel){
if(this.waterLevel!=waterLevel){
this.waterLevel=waterLevel;
notifyObservers();
}
}
public void notifyObservers(){
for(WaterLevelObserver ob : observerArray){
ob.update(waterLevel);
Page 16 of 18
CMJD | Comprehensive Master Java Developer
}
}
}
class Demo{
interface Lion{}
interface Fox extends Lion{}
interface Cat{}
interface Dog extends Lion,Cat{} // Legal
class Lion{}
interface Cat{}
interface Dog{}
class Lion{}
interface Cat extends Lion{} // Illegal
interface Dog implements Lion{} // Illegal
Page 17 of 18
CMJD | Comprehensive Master Java Developer
35. Case XV
interface Vehicle{
public void park();
interface Vehicle{
public void park();
Page 18 of 18