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

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Object-Orientated Design and Programming Unit 9: Abstract Classes and Interfaces Jin Sa.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Computer Science [3] Java Programming II - Laboratory Course Lab 3-1: Creating and Using Interfaces Exception Faculty of Engineering & IT Software Engineering.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Peyman Dodangeh Sharif University of Technology Fall 2014.
Chapter Outline What inheritance is Calling the superclass constructor Overriding superclass methods Protected members Chains of inheritance The Object.
MIT AITI 2004 – Lecture 12 Inheritance. What is Inheritance?  In the real world: We inherit traits from our mother and father. We also inherit traits.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
COMP Inheritance Basics Yi Hong June 09, 2015.
Coming up: Inheritance
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
BY:- TOPS Technologies
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Object-Orientated Analysis, Design and Programming
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Advanced Programming in Java
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Abstract Classes.
Inheritance.
Advanced Programming Behnam Hatami Fall 2017.
Software Engineering System Modeling Extra examples Dr.Doaa Sami
Announcements & Review
انجمن جاواکاپ تقدیم می‌کند دوره برنامه‌نويسی جاوا
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Computer Science II for Majors
Presentation transcript:

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

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; } … …. }

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; } … …. }

Inheritance (cont) 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 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)

SuperClass SubClass Is–a relation

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

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

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; …. }

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

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; } }

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…

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();

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

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();}

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(); } }