Lesson: Inheritance and Polymorphism

 1. 

package org.example3;

public class Parent {
public String name;
public String surname;
public Integer age;

public Parent() {
}

public Parent(String name) {
this.name = name;
}

public Parent(String name, String surname) {
this.name = name;
this.surname = surname;
}

public Parent(String name, String surname, Integer age) {
this.name = name;
this.surname = surname;
this.age = age;
}
}
package org.example3;

public class Child extends Parent{
public String hobby;

public Child() {
super(hobby);
}
}

Burada problem ondan ibaretdir ki, Parent class yaranmamiw Child class yarana bilmez, ve ona gore de

biz hooby state-ni iwlede bilmirik. 


2. super keyword metodlarda:

package org.example3;

public class Parent {
public String name;
public String surname;
public Integer age;

public Parent() {
}

public Parent(String name) {
this.name = name;
}

public Parent(String name, String surname) {
this.name = name;
this.surname = surname;
}

public Parent(String name, String surname, Integer age) {
this.name = name;
this.surname = surname;
this.age = age;
}

public void method() {
System.out.println("method in Parent");
}
}
package org.example3;

public class Child extends Parent{
public String hobby;

public void foo() {
super.method();
}

public void method() {
System.out.println("method in Child");
}
}
package org.example3;

public class Main {
public static void main(String[] args) {
Child child = new Child();
child.method();
child.foo();
}
}

-- diger versiya

package org.example3;

public class Child extends Parent {
public String hobby;

public void foo() {
method();
}
}


-- 

package org.example3;

public class Main {
public static void main(String[] args) {
Child child = new Child();
child.name = "Ali";
child.surname = "Aliyev";
child.age = 12;
foo(child);
}

public static void foo(Parent parent) {
System.out.println(parent.name);
System.out.println(parent.surname);
System.out.println(parent.age);
}
}


-- instanceOf:

package org.example3;

public class Grandchild extends Child{
public String toy;
}
package org.example3;

public class Main {
public static void main(String[] args) {
Grandchild grandchild = new Grandchild();
System.out.println(grandchild instanceof Grandchild);
System.out.println(grandchild instanceof Child);
System.out.println(grandchild instanceof Parent);
}

public static void foo(Parent parent) {
System.out.println(parent.name);
System.out.println(parent.surname);
System.out.println(parent.age);
}
}


-- problem1:

package org.example3;

public class Child2 extends Parent{
public boolean agression;
}
package org.example3;

public class Main {
public static void main(String[] args) {
Child2 child2 = new Child2();
foo(child2);
}

public static void foo(Parent parent) {
Child child = (Child) parent;
System.out.println(child.hobby);
}
}

-- solution:

package org.example3;

public class Main {
public static void main(String[] args) {
Child2 child2 = new Child2();
foo(child2);
}

public static void foo(Parent parent) {
if (parent instanceof Child) {
Child child = (Child) parent;
System.out.println(child.hobby);
} else {
Child2 child2 = (Child2) parent;
System.out.println(child2.agression);
}
}
}


-- Using polymorphism in methods:

package org.example4;

public class Parent {
public void sayHi() {
System.out.println("Parent saying hi...");
}
}
package org.example4;

public class Child1 extends Parent{
}
package org.example4;

public class Child2 extends Parent{
}
package org.example4;

public class Main {
public static void main(String[] args) {
Parent[] parents = {new Child1(), new Child2()};
callAllParents(parents);
}

public static void callAllParents(Parent[] parents) {
for (int i = 0; i < parents.length; i++) {
Parent parent = parents[i];
parent.sayHi();
}
}
}

--- Normal situation

package org.example4;

public class Parent {

public int i = 0;
public void sayHi() {
System.out.println("Parent saying hi...");
}
}
package org.example4;

public class Child1 extends Parent{

public int i = 1;

public void sayHi() {
System.out.println("Child1 saying hi...");
}
}
package org.example4;

public class Child2 extends Parent {

public int i = 2;

public void sayHi() {
System.out.println("Child2 saying hi...");
}
}
package org.example4;

public class Main {
public static void main(String[] args) {
Parent parent = new Parent();
parent.sayHi();
System.out.println(parent.i);

Child1 child1 = new Child1();
child1.sayHi();
System.out.println(child1.i);

Child2 child2 = new Child2();
child2.sayHi();
System.out.println(child2.i);
}
}


-- Seems to be abnormal situation:

package org.example4;

public class Main {
public static void main(String[] args) {
Child1 child1 = new Child1();
child1.sayHi();
System.out.println(child1.i);

Parent p = child1;
p.sayHi();
System.out.println(p.i);
}
}

But it is pretty reasonable if we pay attention to reference, and its place in heap memory.

It is called override!


-- Another problem:

package org.example5;

public class A {

public B foo() {
System.out.println("foo in A");
return new B();
}
}
package org.example5;

public class B extends A {

public A foo() {
System.out.println("foo in B");
return new A();
}
}
package org.example5;

public class Main extends A{
public static void main(String[] args) {
A[] as = {new A(), new B()};
method(as);
}

public static void method(A[] as) {
for(int i = 0; i < as.length; i++) {
A a = as[i];
a.foo();
}
}
}


-- Interesting case:

package org.example6;

public class Father {

public void foo() {
System.out.println("foo in Father");
}

public void method() {
this.foo();
}
}
package org.example6;

public class Son extends Father{

public void foo() {
System.out.println("foo in Son");
}

public void fooSuper() {
super.foo();
}
}
package org.example6;

public class Main {
public static void main(String[] args) {
Father father = new Son();
father.foo();
father.method();
}
}


-- Upcasting and Downcasting izahi:


------------------------------------------------------------------------------------------------------------------------------

                                                            Equals and HashCode

package org.example6;

public class Main {
public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();
System.out.println(o1 == o2);
System.out.println(o1.equals(o2));

String one = new String("test");
String two = new String("test");

System.out.println(one == two);
System.out.println(one.equals(two));

Object oOne = one;
Object oTwo = two;
System.out.println(one == two);
System.out.println(one.equals(two));
}
}


-----------------------------------------------------------------------------------------------------------------------------

package org.lesson11;

public class Employee {
protected int no;
protected String name;
protected int year;
protected String department;
public static final int BASE_SALARY = 500;

public Employee(int no, String name, int year, String department) {
this.no = no;
this.name = name;
this.year = year;
this.department = department;
}

public void work() {
System.out.println("Employee is working...");
}

public double calculateSalary() {
return year * BASE_SALARY;
}

public void printInfo() {
System.out.println("\nNo: " + no);
System.out.println("Name: " + name);
System.out.println("Year: " + year);
System.out.println("Department: " + department);
}
}
package org.lesson11;

public class Manager extends Employee{
protected String departmentManaged;
public static final int MANAGEMENT_PAYMENT = 6000;

public Manager(int no, String name, int year, String department, String departmentManaged) {
super(no, name, year, department);
this.departmentManaged = departmentManaged;
}

public void work() {
System.out.println("Manager is working...");
manage();
}

public void manage() {
System.out.println("Manager manages department: " + departmentManaged);
}

public double calculateSalary() {
return super.calculateSalary() + MANAGEMENT_PAYMENT;
}

public void printInfo() {
super.printInfo();
System.out.println("Managing Department: " + departmentManaged);
}
}
package org.lesson11;

public class Director extends Manager{
protected double bonus;

public Director(int no, String name, int year, String department, String departmentManaged, double bonus) {
super(no, name, year, department, departmentManaged);
this.bonus = bonus;
}

public void work() {
System.out.println("Director is working...");
manage();
}

public void manage() {
System.out.println("Director manages whole company");
makeStrategicPlan();
}

public void makeStrategicPlan() {
System.out.println("Directors makes a strategic plan for the company");
}

public double calculateSalary() {
return super.calculateSalary() + bonus;
}
}
package org.lesson11;

public class Main {
public static void main(String[] args) {
Employee e1 = new Employee(1, "Ali", 8, "Production");
e1.printInfo();
System.out.println("Salary: " + e1.calculateSalary());
e1.work();

System.out.println("--------------------------------------------------------");

Manager m1 = new Manager(2, "Vali", 3, "Production", "Production");
m1.printInfo();
System.out.println("Salary: " + m1.calculateSalary());
m1.work();
m1.manage();

System.out.println("---------------------------------------------------------");

Director d1 = new Director(4, "Test", 28, "Management", "Management", 3000);
d1.printInfo();
System.out.println("Salary: " + d1.calculateSalary());
d1.work();
d1.makeStrategicPlan();
}
}

-----------

package org.lesson11;

public class HR {

public Employee getAnEmployee() {
return new Employee(1, "Ali", 8, "Production");
}
}
package org.lesson11;

public class HRForManagers extends HR{

public Manager getAnEmployee() {
return new Manager(2, "Vali", 3, "Production", "Production");
}
}


*** Covariant return type ***

package org.lesson11;

public class HR {

public Employee getAnEmployee() {
return new Employee(1, "Ali", 8, "Production");
}
}
package org.lesson11;

public class HRForManagers extends HR{

public Manager getAnEmployee() {
return new Manager(2, "Vali", 3, "Production", "Production");
}
}


------------------------------------------------------------------------------------------------------------------------------

toString() metodu

Mes: Car car = new Car(...);

System.out.println(car);

System.out.println(car.toString());

Bu iki method ayni iwi gorur.




















Комментарии

Популярные сообщения из этого блога

IoC:ApplicationContext, BeanFactory. Bean

Lesson1: JDK, JVM, JRE

Lesson_2: Operations in Java