Lab4: Object-Oriented Programming Concept
LAB OBJECTIVE: Deep understanding about Object-Oriented Programming concepts and its paradigm.
Polymorphism in Java
Polymorphism reduces the complexity of an object. It can only be achieved via the behavior of an object, properties of an object do not play any role in the case of polymorphism.
Achieving polymorphism via function overloading, can be done by changing only in no. of arguments
//save as: FunctionOverloading.java
class FunctionOverloading{
void show(){
System.out.println("hello");
}
void show(int x){
System.out.println("Hello by calling single arguments");
}
void show(int x,int y){
System.out.println("Hello by calling double arguments");
}
void show(int x, char y, long L){ // method calling by different data types
System.out.println("Hello by calling tripple arguments");
}
public static void main(String args\[\]) {
FunctionOverloading fl = new FunctionOverloading();
fl.show(10); // method calling via an argument
fl.show(10,10); //method calling via double arguments
fl.show(10,'A',100L);
} // end of main ()
} // end of class
Output
javac FunctionOverloading.java // for compilaton
Java FunctionOverloading // To run
Hello by calling single arguments
Hello by calling double arguments
Hello by calling triple arguments
Via changing the data type of their arguments to achieve Function Overloading
//save as: FunctionOverloading1.java
class FunctionOverloading1{
void show(){
System.out.println("hello");
}
void show(int x){
System.out.println("hello integer");
}
void show(long x){
System.out.println("Hello by long");
}
void show(char x){
System.out.println("Hello by Character");
}
public static void main(String args\[\])
{
FunctionOverloading1 fl = new FunctionOverloading1();
fl.show(10);
fl.show(10L);
fl.show('z');
}
}
Output
javac FunctionOverloading1.java
java FunctionOverloading1
Hello by long
Hello Integer
Hello by character
Scenario of why ” this”
Case 0:
//save as: This.java
class This{
int x=10;
void show(int y){
System.out.println(x);
System.out.println(y);
}
public static void main(String arguments\[\]){
This t= new This();
t.show(20);
}
}
/\*Output:
Javac This.java
Java This
10
20
\*/
Case 1:
//save as: This1.java
class This1{
int x=10;
void show(int x){
System.out.println(x);
System.out.println(x);
}
public static void main(String arguments\[\]){
This1 t= new This1();
t.show(20);
}
}
/\*note: priority always goes to local variables.
Output:
Javac This1.java
Java This1
20
20
\*/
Case 2:
//save as: This2.java
class This2{
int x=10;
void show(int x,This2 z){
System.out.println(x);
System.out.println(z.x);
}
public static void main(String arguments\[\]){
This2 t= new This2();
t.show(20,t);
System.out.println(t.x);
}
}
/\*
Output:
javac This2.java
java This2
20
10
10
\*/
Case 3:
//save as: This3.java
class This3{
int x=10;
// "this is the feature of oops not java"
void show(This3 this, int y){
// "this" is always used to refer current object which is being used not creating or created.
System.out.println(this.x);
System.out.println(y);
}
public static void main(String arguments\[\]){
This3 t= new This3();
t.show(20);
}
}
/\*
Output:
javac This3.java
javac This3
10
20
\*/
Case 4:
This example proved that if you add “this” or remove “this”, does not matter at all because JVM implicitly add “this”, hence no error occurs.
//save as: This4.java
class This4{
int x=10;
// (This this, int y ) by default will be added by JVM
void show(int y){
System.out.println(this.x);
System.out.println(y);
}
public static void main(String arguments\[\]){
This4 t= new This4();
t.show(20);
}
}
Output
javac This4.java
java This4
10
20
'this'
is a special keyword which refers to the current object means an object which is being used to access the data or call the method
Constructor in Java
A Java constructor
is a special method that is called when an object is instantiated. In other words, when you use the new keyword. The purpose of a Java constructor is to initialize the newly created object before it is used.
- Constructor as class name
- It does not have any return type
- Cannot be called explicitly
Case 0:
//save as: Employee.java
class Employee{
int salary;
Employee() { //Constructor
salary =15000;
System.out.println(salary);
}
public static void main(String arguments\[\]){
// Anonymous object creation
new Employee(); //”new” operator is used to allocate new memory in heap area
}
}
Output
javac Employee.java
java Employee
15000
In java, every class has a constructor by default which is known as default constructor. A constructor with no parameter known as a non-parameterized constructor, if you don’t write a constructor explicitly then a default constructor will automatically added in a class by JVM.
Case 1:
// save as: Constructor.java
class Constructor{
int salary;
Constructor(){
this.salary=15000;
System.out.println(salary);
}
public static void main(String argument\[\]){
new Constructor(); //constructor will run before anonymous object executed
new Constructor(); //constructor will run before anonymous object executed
}
}
/\*
Output:
javac Constructor.java
java Constructor
15000
15000
\*/
Case 2: When to use default constructor
//save as Temp.java
class Temp{
int x;
int y;
Temp(){ // default constructor
x=10;
y=20;
}
void show(){
System.out.println(x);
System.out.println(y);
}
public static void main(String...s){
Temp t= new Temp(); // object creation
t.show();
Temp t1=new Temp();
t1.show();
}
}
/\*
Output:
Javac Temp.java
Java Temp
10
20
10
20
\*/
Case 3: Parameterized Constructor
Whenever you want to initialize data members for each object with a different value then always use parameterize constructor.
//save as Temp1.java
class Temp1{
int x; // instance variable
int y;
Temp1(int x,int y){ // Parameterised Constructor
this.x=x;
this.y=y;
}
void show(){
System.out.pritnln(x);
System.out.println(y);
}
public static void main(String args\[\]) {
new Temp1(10,20); // passing the argument of int type
}
}
/\*
Output:
Javac Temp1.java
Java Temp1
10
20
\*/
Init Block
Init block
is always executed before any constructor whenever that constructor is used for creating a new object. Instance Initialization Blocks or IIB are used to initialize instance variables.
//save as InitBlock.java
class InitBlock{
{ // opening of init\_Block
System.out.println("Hello, Rstians! i am init Block");
} // end of init\_Block
InitBlock() { // constructor
System.out.println("default constructor");
}
public static void main(String args\[\]){
new InitBlock(); // Anonymous object
}
}
/\*
Output:
Javac InitBlock.java
Java InitBlock
Hello, Rstians! i am init Block
default constructor
\*/
Static Block
A static block
is used for initializing the static variables. This block gets executed when the class is loaded in the memory. A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.
//save as StaticBlock.java
class StaticBlock{
static String countryName;
// this block will always executed whether constructor runs or not
static{
countryName = "INDIA";
}
static{
System.out.println("hello i'm static block from "+countryName );
}
public static void main(String ...s){
new StaticBlock();
}
}
Output
javac StaticBlock.java
java StaticBlock
hello i'm static block from INDIA
Inheritance in Java
Inheritance
in Java is a mechanism in which one object acquires all the data members and methods of a parent class object. Inheritance is a concept that is used to reusability among the objects. When you inherit from an existing class, you can reuse methods and fields of the parent class. It is used to achieve dynamic binding, by default all the data members and member function of a parent class are available to child class if they are not private.
Case 0:
//save as Child.java
class Base{
int x; int y;
void show(){
System.out.println(x);
System.out.println(y);
}
}
class Child extends Base { //"extends" is a keyword to make Is a relationship b/w objects
void get(int x,int y){
this.x=x;
this.y=y;
}
public static void main(String ...s){
Child c1=new Child();
c1.get(10,20); // passing the int parameter
c1.show();
}
}
/\*
Output:
javac Child.java
java Child
10
20
\*/
Case 1: For private data member
//save as Child1.java
class Base{
private int x=20;
void show(){
System.out.println(x);
}
}
class Child1 extends Base{ // ‘extends’ keyword to make connection between two objects
public static void main(String...s){
Child1 c1=new Child1();
// System.out.println(c1.x);
c1.show();
}
}
/\*
Output:
javac Child1.java
java Child1
10
20
\*/
Case 2: Data Hiding:
//save as Child2.java
class Parent{
int x=50; // instance variable of Parent
}
class Child2 extends Base{
int x=20; // instance variable of Child2
void show(){
System.out.println(x);
// System.out.println( super.x ); // ‘super’ keyword used to access parent data member
}
public static void main(String...s){
Child2 c1=new Child2();
c1.show();
}
}
/\*
Output:
javac Child2.java
java Child2
20
\*/
Case 3: Multilevel Inheritance:
//save as
class Base extends Dadaji{
int x=50;
}
class Dadaji{
int x=70;
}
class Child3 extends Base{
int x=20;
//int y=20;
void show(){
int x = 10;
System.out.println(x); // priority always goes to local variable
System.out.println(this.x);
System.out.println(super.x); // 'super' used to access parent class data member
System.out.println(((Dadaji)this).x); // accessing to parent to parent class data member
}
public static void main(String...s){
Child3 c1=new Child3();
c1.show();
}
}
/\*
Output:
javac Child3.java
java Child3
10
20
50
70
\*/
Case 4: Function Inheritance
// save as Child.java
class Base{
void show(){
System.out.println("Base");
}
}
class Child extends Base{
void show(){
System.out.println("Child");
super.show(); // to call parent class method
}
public static void main(String ...s){
Child c=new Child();
c.show();
}
}
/\*
Output:
F:\\coreJAVA\\Inheritance\\member function inheritance>javac Child.java
F:\\coreJAVA\\Inheritance\\member function inheritance>java Child
Child
Base
\*/
Case 5: Function Overriding via different return type
//save as Child2.java
class Base{
A show( ){
System.out.println("Base");
return new A( );
}
}
class Child2 extends Base{
B show( ){
System.out.println("Child");
super.show( );
return new B( );
}
public static void main(String ...s){
Child2 c=new Child2( );
c.show( );
}
}
class A{
}
class B extends A{
}
Output
javac Child2.java
java Child2
Child
Base
Case 6: Upcasting in java
//save as Child5.java
/\*
Keeping the reference id of child class into the reference variable of parent class
\*/
class Base{
void show( ){
System.out.println("Base");
}
}
class Child5 extends Base{
void show( ){
System.out.println("Child");
}
public static void main(String ...s){ // another way of writing main() method
Base c=new Child5( ); // upcasting
c.show( );
}
}
Output
javac Child5.java
java Child5
Child
Constructor Inheritance in Java
Case 0: Default Constructor
// save as C.java
class A{
A( ){
// super(); by default
System.out.println("A");
}
}
class B extends A{
B( ){
// super(); by default
System.out.println("B");
}
}
class C extends B{
C( ){
// super(); by default
System.out.println("C");
}
public static void main(String arguments\[\]){
new C( );
}
}
Output
Javac C.java
java C
A
B
C
Case 1: Parameterized Constructor
//save as C1.java
class A{
A(){
System.out.println("A");
}
}
class B extends A{
B(int x){
super();
System.out.println("default");
}
B(){
super();
System.out.println("B");
}
}
class C1 extends B{
C1(){
super(10);
System.out.println("C");
}
C1(int x){
super();
System.out.println(x);
}
public static void main(String arguments\[\]){
new C1();
new C1(20);
}
}
Output
Javac C1.java
java C1
A
Default
C
A
B
20
Abstraction in Java
An abstract class
is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
public abstract class GraphicObject {
public abstract class GraphicObject {
// declare fields
// declare non abstract methods
abstract void draw(); // abstract method
}
Case 0: For instance
//save as Child .java
/\*
‘abstract’ is a keyword to declare abstract classes, methods as abstract
\*/
abstract class Base{
int x;
int y;
void show(){
System.out.println(x);
System.out.println(y);
}
abstract int add(int x, int y);
}
class Child extends Base {
int add(int x, int y){
return x+y;
}
public static void main(String args\[\]){
Child c1=new Child();
int res = c1.add(10,20);
System.out.println(res);
c1.show();
}
}
Output
javac Child.java
java Child
30
0
0
Case 1: Practice Question
//save as Child1 .java
abstract class Base{ // abstract class
int x;
int y;
void show(){
System.out.println(x);
System.out.println(y);
}
abstract void display(); // abstract method
}
class Child1 extends Base {
void get(int x, int y){
this.x=x;
this.y=y;
}
void display(){
System.out.println("display");
}
public static void main(String args\[\]){
Child1 c1=new Child1();
c1.get(10,20);
c1.show();
c1.display();
}
}
/\*
kam kisi aur ka, karna kisi aur ko
\*/
Case 2: Process of Registration
In case of registration, a child class has to register itself with parent class by passing its reference to a parent class.
//save as Child2 .java
abstract class Base{
int x;
int y;
abstract void display();
void registration(Base b){ // resistration method
System.out.println("Jake display method ko chala na");
b.display();
}
}
class Child2 extends Base {
void display(){
System.out.println("Display");
}
public static void main(String args\[\]){
Base c1=new Child2(); // upcasting
c1.registration(c1); // doing restration
}
}
Output
javac Child2.java
java Child2
Jake display method ko chala na
Display
Interface in Java
Interfaces
are the contract between a java programmer and a java programming language. it is implicitly abstract so that it cannot be instantiated.
Case 0: Creation of Interface
//save as Child.java
interface My{ // ‘interface’ is keyword to make interface
void show();
}
class Child implements My{ // ‘implements’ is keyword to use or implement interface
public void show(){
System.out.println("show");
}
public static void main(String args\[\]){
Child c1=new Child();
Temp t=new Temp();
t.register(c1);// doing registration with interface My
}
}
class Temp{
public void register(My m){
m.show();// calling show method
}
}
Output
Javac Child.java
java Child
show
Case 1: Child class can implements more than one interface simultaneously
//save as Child2 .java
interface My{
void show();
}
interface My1{
void display();
}
class Child2 implements My,My1 { // implementing more than one interface
public void show(){
System.out.println("show");
}
public void display(){
System.out.println("display");
}
public static void main(String args\[\]){
My m=new Child2(); // upcasting
m.show();
My1 m1=new Child2(); // upcasting
m1.display();
}
}
Output:
F:\\coreJAVA\\7Interface>javac Child2.java
F:\\coreJAVA\\7Interface>java Child2
show
display
\*/
Case 2: If child class is getting the same default method from more than one then it has to override that method otherwise it’s a compilation error.
//save as Child5 .java
interface My{
public default void show(){
System.out.println("My");
}
}
interface My1{
public default void show(){
System.out.println("My1");
}
}
class Child5 implements My,My1{ // implementing more than one interface
public void show(){
System.out.println("Child");
}
public static void main(String args\[\]){
My m=new Child5 ();
m.show();
My1 m1=new Child5 ();
m1.show ();
}
}
Case 3: Static method of an interface inherited in child class can only be access via interface name.
//save as Child6 .java
interface My{
public static void show() { // show
System.out.println("My");
}
}
class Child6 implements My{
public static void main(String ...s){
My.show(); // My is a variable of type interface
}
}
/\*
Output:
F:\\coreJAVA\\7Interface>javac Child6.java
F:\\coreJAVA\\7Interface>java Child6
My
\*/
Aggregation in Java
//save as Emp.java
class Address{
String city;
String state;
String country;
public Address(String city, String state, String country){
this.city = city;
this.state = state;
this.country = country;
}
}
public class Emp{
int id;
String name;
Address address;
public Emp(int id, String name,Address address){
this.id = id;
this.name = name;
this.address=address;
}
void display(){
System.out.println(id+" "+name);
System.out.println(address.city+" "+address.state+" "+address.country);
}
public static void main(String\[\] args) {
Address address1=new Address("New delhi","Delhi","india");
Address address2=new Address("gno","UP","india");
Emp e=new Emp(111,"VIVEK",address1);
Emp e2=new Emp(112,"arun",address2);
e.display();
e2.display();
}
}
Output
javac Emp.java
java Emp
111 VIVEK
New delhi Delhi indi
112 arun
gno UP india