Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science [3] Java Programming II - Laboratory Course Lab 1 + 2: Review inheritance & abstract Review inheritance & abstractPolymorphism Faculty.

Similar presentations


Presentation on theme: "Computer Science [3] Java Programming II - Laboratory Course Lab 1 + 2: Review inheritance & abstract Review inheritance & abstractPolymorphism Faculty."— Presentation transcript:

1 Computer Science [3] Java Programming II - Laboratory Course Lab 1 + 2: Review inheritance & abstract Review inheritance & abstractPolymorphism Faculty of Engineering & IT Software Engineering Department WWW.PALINFONET.COM Eng.Omar Al-Nahal Eng.Omar Al-Nahal

2 Inheritance What the importance of inheritance? public class Student { private String name; private String idNum; private String gender; private double mark; public Student (String name,String idNum, String gender, double mark){ this.name=name; this.idNum=idNum; this.gender=gender; this.mark=mark; } public void setName(String name){ this.name=name;} …. … public String getName(){ return name; } … …. }

3 Inheritance (cont) public class Employee { private String name; private String idNum; private String gender; private double salary ; public Employee (String name,String idNum, String gender, double salary){ this.name=name; this.idNum=idNum; this.gender=gender; this.salary=salary; } public void setName(String name){ this.name=name;} …. … public String getName(){ return name; } … …. }

4 Inheritance (cont) Inheritance (cont)

5 public class Person { private String name; private String idNum; private String gender; public Person (String name,String idNum, String gender){ this.name=name; this.idNum=idNum; this.gender=gender; } public void setName(String name){ this.name=name;} …. … public String getName(){ return name; } … …. } public class Student extends Person { private double mark; public Student (String name, String idNum, String gender, double mark){ super (name, idName, gender); this.mark=mark; } public void setMark(double mark){ this.mark=mark;} public double getMark( ){ return mark;}} public class Employee extends Person { private double salary; public Employee (String name, String idNum, String gender, double salary){ super (name, idName, gender); this. salary=salary; } public void setSalary(double salary){ this. salary=salary;} public double getSalary( ){ return salary;}} Inheritance (cont) Inheritance (cont)

6 SuperClass SubClass Is–a relation

7 Inheritance (cont) public class Student extends Person { private double mark; public Student (String name, String idNum, String gender, double mark){ super (name, idName, gender); this.mark=mark; } public void setMark(double mark){ this.mark=mark;} public double getMark( ){ return mark;} } public class Person { private String name; private String idNum; private String gender; public Person (String name,String idNum, String gender){ this.name=name; this.idNum=idNum; this.gender=gender; } public void setName(String name){ this.name=name;} public String getName(){ return name; } …. … public String toString(){ return "The name of the person is "+ getName();} } Student st=new Student(“noor”,”11”, ”female”,90.0); System.out.println (st. toString()); The name of the person is noor

8 Inheritance (cont) public class Person { private String name; private String idNum; private String gender; public Person (String name,String idNum, String gender){ this.name=name; this.idNum=idNum; this.gender=gender; } public void setName(String name){ this.name=name;} public String getName(){ return name; } …. … public String toString(){ return "The name of the person is"+getName();} } public class Student extends Person { private double mark; public Student (String name, String idNum, String gender, double mark){ super (name, idName, gender); this.mark=mark; } public void setMark(double mark){ this.mark=mark;} public double getMark( ){ return mark;} public String toString(){ return super.toString()+". Her mark is "+getMark(); } } Student st=new Student(“noor”,”11”, ”female”,90.0); System.out.println (st. toString()); The name of the person is noor. Her mark is 90.0

9 Inheritance (cont) Overloading deals with multiple methods in the same class with the same name but different signatures. Ex: public int operation( int x); public int operation (int x,int y); public int operation( double x); public int operation (String x,int y); public int operation (int x,String y); public double operation( int x); Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature. b All classes are derived from the Object class. public class Person { private String name; …. } public class Person extends Object { private String name; …. }

10 Inheritance (cont) Abstract method: which have no code. Public abstract int getArea(); Abstract class: which have abstract methods and concrete methods. public abstract class Shape{ private String name; public Shape ( String name){ this.name=name; } public abstract String getName(); public abstract double getArea(); } An abstract class cannot be instantiated.Shape sh=new Shape(“ccc”); If class inherit abstract class, it must override all abstract methods

11 Inheritance (cont) public class Circle extends Shape{ private double r; public Circle ( double r){ super(“Circle”); this.r=r; } public String getName(){ return “Circle”;} public double getArea(){ Return Math.PI*r*r;} } public class Rectangle extends Shape{ private double height; private double width; public Circle ( double h,double w){ super(“Rectangle”); height=h; width=w; } public String getName(){ return “Rectangle”;} public double getArea(){ Return height*width; } }

12 Inheritance (cont) public class Test{ public static void main (String args[]){ Circle C=new Circl1(2.5); Rectangle R=new Rectangle(4.0,5.0); System.out.println( “The shape name is “+C.getName()+ “with Area”+C.getArea(); System.out.println( “The shape name is “+R.getName()+ “with Area”+R.getArea(); } Now Try…

13 b Polymorphism means many forms or many shapes. b A variable of a superclass type can reference an object of a subclass type Polymorphism Animal CatDogRabbit Animal anim1=new Animal(); Animal anim2 = new Cat(); Animal anim3=new Dog(); Animal anim4=new Rabbit(); Cat c= new Animal();

14 Dog Move() Polymorphism (cont) Cat Move() Animal Move() Rabbit Move() Animal anim2 = new Cat(); Anim2.move();

15 Dog move() Polymorphism (cont) Cat move() eat() Animal Move() Rabbit move() Animal anim2 = new Cat(); anim2.move(); anim2.eat(); Animal anim2 = new Cat(); If(anim2 instanceOf Cat){ Cat C1 =(Cat) anim2; C1.eat();}

16 public class Test{ public static void main (String args[]){ Shape sh[]=new Shape[2]; sh[0]=new Circl1(2.5); sh[1] =new Rectangle(4.0,5.0); for( int i=0;i<sh.length;i++) System.out.println( “The shape name is “+sh[i].getName()+ “with Area”+sh[i].getArea(); }} Polymorphism (cont) public class Test{ public static void main (String args[]){ Circle C=new Circl1(2.5); Rectangle R=new Rectangle(4.0,5.0); System.out.println( “The shape name is “+C.getName()+ “with Area”+C.getArea(); System.out.println( “The shape name is “+R.getName()+ “with Area”+R.getArea(); } }


Download ppt "Computer Science [3] Java Programming II - Laboratory Course Lab 1 + 2: Review inheritance & abstract Review inheritance & abstractPolymorphism Faculty."

Similar presentations


Ads by Google