Object Oriented Programming in JAVA

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
23-May-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
15-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
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.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Chapter 10 Inheritance and Polymorphism
Inheritance and Access Control CS 162 (Summer 2009)
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Classes - Intermediate
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse.
Topics Instance variables, set and get methods Encapsulation
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
UNIT-2. Inheritance –Definition Single Inheritance Benefits of inheritance Member access rules super classes Polymorphism Method overriding Using final.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Inheritance class TwoDShape { private double width;
Data Structures and Algorithms
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
UNIT-2.
Polymorphism 11-Nov-18.
Modern Programming Tools And Techniques-I Inheritance
OOP’S Concepts in C#.Net
Polymorphism 15-Nov-18.
Java Programming Language
Polymorphism 28-Nov-18.
INHERITANCE.
Java Programming, Second Edition
Inheritance CT1513.
Inheritance.
Index Understanding static Final Inheritance Super
Polymorphism 15-Apr-19.
Polymorphism 21-Apr-19.
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Object Oriented Programming in JAVA By:Srwa Mohammad

The Java Oops Object Oriented Programming (OOP) is a programming concept used in several modern programming languages, like C++, Java and Python. Before Object Oriented Programming programs were viewed as procedures that accepted data and produced an output. What is Class: Java is a purly an Object Oriented Language. Everything in java is an object. A Class does not hold any value unless and until it is used for object creation. Definition: A class is a collection of data, stored in named fields, and code, organized into named methods, that operates on that data.

The Java Class with method and parameter class rectangle { double l,w; void get() { l=5; w=5; } class Callmethod { public static void main(String args[]) rectangle r=new rectangle(); r.get(); r.area(); } void area() { double a; a=l*w; System.out.println("Area is:"+a); }}

The Java Constructor Constructor:- A Constructor is a special method that does not have a return type and a has the same name as that of the class name. It means that a constructor can act as a normal method or function but it cannot return any value. Every class must have at least one constructor. If there are no constructors for your class, the compiler will supply a default constructor (no-arg constructor). A constructor is used to construct an object. A constructor looks like a method and is sometimes called a constructor method. A constructor never returns a value

The Java Default Constructor Example class box { int l,w; box() { l=5; w=5; } void show() System.out.println("Length is:"+l); System.out.println("width is:"+w); } } class GradeBook public static void main(String args[]) box b=new box(); b.show(); } }

Inheritance  enables new objects to take on the properties of existing objects . A class that is used as the basis for inheritance is called a superclass or base class. A class that inherits from a superclass is called a subclass . Its done by using (extends) keyword

inheritance

Inheritance in Java class A { int i;int j; void showij(){ System.out.println("i and j:" +i+" "+j); }} class B extends A { int k; void showk(){ System.out.println("K:" +k); } void sum(){ System.out.println("i+j+k:" +(i+j+k)); 2/3/2015 Object Oriented Programming using JAVA 81

Inheritance in Java… class SimpleInheritanc{ public static void main(String args[]){ A objectA=new A(); B objectB=new B(); objectA.i=10 ; objectA.j=20; System.out.println("Contents of objectA :”); objectA.showij(); System.out.println(); objectB.i=7; objectB.j=8; objectB.k=9; System.out.println("contents of objectB“); objectB.showij(); objectB.showk(); System.out.println(); System.out.println("Sum of i,j and k in objectB :”); objectB.sum(); }} Object Oriented Programming using JAVA 82

Compiler determines which method has to execute automatically. Method Overloading In java we can define many number of methods in a class with same name. Defining two or more method with same name is known as “Method overloading”. Compiler determines which method has to execute automatically. 2/3/2015 Object Oriented Programming using JAVA 72

Method Overloading Example class Rectangle { void area(int length, int width ){ int a= length * width; System.out.println("Area of Rectangle : " + a) } void area(double length, double width) { double b= length * width; System.out.println("Area ofRectangle : " + b);}} class RectangleDemo { public static void main(String args[]) { Rectangle r1 = new Rectangle(); r1.area(10, 20); r1.area(10.50, 20.50); }} 2/3/2015 Object Oriented Programming using JAVA 73

Method Overriding Method overriding: refers to idea of having a subclass contain a method with same name and parameters as a method in super class Static methods can not be overriding

Overriding exmple class AA{ public void same(){ System.out.println(“this is class AA”); }} class BB extends AA{ System.out.println(“this is class BB”); public class Aga extends BB{ public static void main(String[]args){ Aga x=new Aga(); x.same()

overridden method using super keyword If you write super.func() to call the method, it will call the method that was defined in the superclass. class AA{ public void same(){ System.out.println(“this is class AA”); }} class BB extends AA{ System.out.println(“this is class BB”); public class Aga extends BB{ super.same(); System.out.println(“this is class Aga”); } public static void main(String[]args){ Aga x=new Aga(); x.same()

polymorphism Polymorphism: "Poly" means "many" and "morph" means "form". Polymorphism(many forms) is the ability of an object to become many different forms of object. Example: overloading, overriding, virtual functions We use polymorphism with inheritance and, and the concept of it is “ one interface , multiple methods”.

2/3/2015 Object Oriented Programming using JAVA 109